c# - Linqpad 图表。 Column 和 StackedColumn 的组合

标签 c# .net charts data-visualization linqpad

我正在尝试在 LinqPad 中制作一些图表。我有一些来自 api 的日志,我知道调用了什么 api 以及请求是否被缓存(在我们的实际情况下,我们使用来自 bing api 的坐标解析地址,或者如果我们缓存了它,我们从缓存表中获取地址)

我使用这个 linqpad 脚本:

var startDate = new DateTime(2019, 1,1);

var Requests = new[]
{
    new {Date=startDate, Name = "Api1", Cached=true },  
    new {Date=startDate, Name = "Api2", Cached=true },  
    new {Date=startDate, Name = "Api3", Cached=true },  
    new {Date=startDate, Name = "Api1", Cached=true },  
    new {Date=startDate, Name = "Api1", Cached=false }, 
    new {Date=startDate, Name = "Api2", Cached=false }, 
    new {Date=startDate, Name = "Api3", Cached=false }, 
    new {Date=startDate, Name = "Api1", Cached=false }, 
    new {Date=startDate.AddDays(1), Name = "Api3", Cached=true },   
    new {Date=startDate.AddDays(1), Name = "Api1", Cached=false },  
    new {Date=startDate.AddDays(1), Name = "Api2", Cached=true },   
    new {Date=startDate.AddDays(1), Name = "Api2", Cached=false },  
    new {Date=startDate.AddDays(1), Name = "Api1", Cached=true },   
    new {Date=startDate.AddDays(1), Name = "Api1", Cached=false },  
    new {Date=startDate.AddDays(1), Name = "Api3", Cached=true },   
};

Requests.GroupBy(x=>x.Date).Chart (c => c.Key)
    .AddYSeries (c => c.Count(x=>x.Name=="Api1"),name:"Api1")
    .AddYSeries (c => c.Count(x=>x.Name=="Api2"),name:"Api2")
    .AddYSeries (c => c.Count(x=>x.Name=="Api3"),name:"Api3")
    .AddYSeries (c => c.Count(x=>x.Name=="Api1" && x.Cached),name: "Api1 Cached")
    .AddYSeries (c => c.Count(x=>x.Name=="Api2" && x.Cached),name: "Api2 Cached")
    .AddYSeries (c => c.Count(x=>x.Name=="Api3" && x.Cached),name: "Api3 Cached")   
    .Dump();

结果是:

enter image description here

实际上我希望每天只有 3,000 列,但必须放宽它们(以便在一列中同时显示总值和缓存值)

如果我切换到 SlackedColumns,我的所有值都在一列中,这不是我想要的:

enter image description here

任何想法,如何去做?

更新:

我想要的是这样的(但我更喜欢,它是按日期分组的,就像 linqpad 那样):

enter image description here

最佳答案

我无法用 linqpad 做到这一点,所以我使用外部库在 html 中获得所需的输出:

所以看起来像这样:

enter image description here

void Main()
{
    var startDate = new DateTime(2019, 1,1);

    var Requests = new[]
    {
        new {Date=startDate, Name = "Api1", Cached=true },  
        new {Date=startDate, Name = "Api2", Cached=true },  
        new {Date=startDate, Name = "Api3", Cached=true },  
        new {Date=startDate, Name = "Api1", Cached=true },  
        new {Date=startDate, Name = "Api1", Cached=false }, 
        new {Date=startDate, Name = "Api2", Cached=false }, 
        new {Date=startDate, Name = "Api3", Cached=false }, 
        new {Date=startDate, Name = "Api1", Cached=false }, 
        new {Date=startDate.AddDays(1), Name = "Api3", Cached=true },   
        new {Date=startDate.AddDays(1), Name = "Api1", Cached=false },  
        new {Date=startDate.AddDays(1), Name = "Api2", Cached=true },   
        new {Date=startDate.AddDays(1), Name = "Api2", Cached=false },  
        new {Date=startDate.AddDays(1), Name = "Api1", Cached=true },   
        new {Date=startDate.AddDays(1), Name = "Api1", Cached=false },  
        new {Date=startDate.AddDays(1), Name = "Api3", Cached=true },   
    };



    var data = new Dictionary<Tuple<string, bool>,List<int>>(); 

    foreach (var val in Requests.GroupBy(x=>x.Date.ToShortDateString()))
        {
            var keyCached = Tuple.Create(val.Key, true);
            var keyNotCached = Tuple.Create(val.Key, false);

            if (!data.ContainsKey(keyCached))
            {
                data.Add(keyCached, new List<int>());
            }           
            if (!data.ContainsKey(keyNotCached))
            {
                data.Add(keyNotCached, new List<int>());
            }                       

            data[keyCached].Add(val.Count(x=>x.Cached));
            data[keyNotCached].Add(val.Count(x=>!x.Cached));            
        }



    var columns = Requests.Select(c=>c.Date.ToShortDateString());       
    var rawData= data.Select(x=>new {name =x.Key.Item1 + " " + ( x.Key.Item2 ? "Cached":"Not Cached"), stack = x.Key.Item1, data = x.Value});       
    Util.RawHtml(createHtml(columns, Newtonsoft.Json.JsonConvert.SerializeObject(rawData))).Dump();         

}

private string createHtml(IEnumerable<string> columns, string serializedData)
{
var columnsString = Newtonsoft.Json.JsonConvert.SerializeObject(columns);
var s = @"<script src=""https://code.highcharts.com/highcharts.js""></script>
<script src=""https://code.highcharts.com/modules/exporting.js""></script>
<script src=""https://code.highcharts.com/modules/export-data.js""></script>

<div id=""container"" style=""min-width: 310px; height: 400px; margin: 0 auto""></div>

<script>
Highcharts.chart('container', {

    chart: {
        type: 'column'
    },

    title: {
        text: 'Total'
    },

    xAxis: {
        categories:"+columnsString+@"
    },

    yAxis: {
        allowDecimals: false,
        min: 0,
        title: {
            text: 'Number of calls'
        }
    },

    tooltip: {
        formatter: function () {
            return '<b>' + this.x + '</b><br/>' +
                this.series.name + ': ' + this.y + '<br/>' +
                'Total: ' + this.point.stackTotal;
        }
    },

    plotOptions: {
        column: {
            stacking: 'normal'
        }
    },

    series: "+serializedData+@"
});
</script>";

return s;

}

关于c# - Linqpad 图表。 Column 和 StackedColumn 的组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56703076/

相关文章:

charts - 仪表图最大值

c# - 在windows服务中设置端口号

c# - 将三个数组根据其索引合并为单个数组

c# - ORA-12545 : Connect failed because target host or object does not exist - No error on System. Data.OracleClient

c# - 在 LINQ 之外使用匿名类型是一件好事吗?

javascript - 显示选定区域的值(JavaScript 图表)

apache-flex - 使 Flex 在 AreaSeries 中显示最近的数据提示

重复列表条目的 C# 问题

.net - Entity Framework - 在结束使用后保留加载/包含的相关对象?

c# - 使用无效的 SSL 证书从 WSDL 生成服务代理时出现问题