简体   繁体   English

C#HighCharts X轴

[英]C# HighCharts X Axis

I am using Highcharts to display information as a line chart. 我正在使用Highcharts将信息显示为折线图。

I have 2 lines. 我有两行。 One represents every half hour, and the other represents every hour. 一个代表每半小时,另一个代表每小时。

在此处输入图片说明 Now, the x-axis is based on the half hour line.. but my concern is with the blue hour line.. There are 24 dots, but for example on the 2nd point when hovering on it, it says point 0.5 , when it should be 1 .. since there aren't half hour times on the hour line.. Realistically I would like the blue hour line to extend all the way across the chart and only having points on the hour ticks.. not half hour ticks.. 现在,x轴是基于半小时线的。.但是我担心的是蓝色小时线。.有24个点,但是例如,当鼠标悬停在第二点时,它表示为0.5点,应该是1 ..因为小时线上没有半小时的时间。.实际上,我希望蓝色小时线一直延伸到整个图表,并且只在小时刻度上有点..而不是半小时刻度。 。

Here is my implementation: 这是我的实现:

var lstXAxis = db.DailyRecords.Where(x => x.RecordDateTime.Year == year && !x.deleted)
    .GroupBy(x => x.RecordDateTime.Hour + (x.RecordDateTime.Minute >= 30 ? 0.5 : 0))
    .Select(x => x.Key).OrderBy(x => x).ToList();

var lstHalfHoursAsString = lstXAxis.Select(x => x.ToString()).ToArray(); // results in 48 objects which is correct

var lstGroupRecordsByHour =
    db.DailyRecords.Where(x => x.RecordDateTime.Year == year && !x.deleted)
        .GroupBy(x => x.RecordDateTime.Hour).ToList(); // results in 24 which is correct

var arrySummariesByHour = lstGroupSummariesByHour.Select(x => new object[] { x.Count() }).ToArray();

var lstSummariesByHalfHour = db.DailyRecords.Where(x => x.RecordDateTime.Year == year && !x.deleted)
    .GroupBy(x => x.RecordDateTime.Hour + (x.RecordDateTime.Minute >= 30 ? 0.5 : 0)).ToList();

var arrySummariesByHalfHour = lstSummariesByHalfHour.Select(x => new object[] { x.Count() }).ToArray();

Highcharts countHourChart =
    new Highcharts("ChartTwo").InitChart(new DotNet.Highcharts.Options.Chart()
    {
        DefaultSeriesType = DotNet.Highcharts.Enums.ChartTypes.Line,
        Width = 1180
    })
        .SetCredits(new Credits { Enabled = false })
        .SetTitle(new Title
        {
            Text = year.ToString() + " Count Graph By Hour",
            Style = "font: 'normal 20px Impact', color: 'black'"
        })
        .SetXAxis(new XAxis
        {
            Categories = lstHalfHoursAsString,
            Title = new XAxisTitle { Text = "Hour", Style = "font: 'normal 14px Arial', color: 'black'" }
        })
        .SetYAxis(new YAxis
        {
            Title =
                new YAxisTitle
                {
                    Text = "Mission Count",
                    Style = "font: 'normal 14px Arial', color: 'black'"
                }
        })
        .SetPlotOptions(new PlotOptions
        {
            Column = new PlotOptionsColumn
            {
                DataLabels = new PlotOptionsColumnDataLabels
                {
                    Enabled = true
                },
                EnableMouseTracking = false
            }
        })
        .SetSeries(new[]
        {
            new Series {Data = new Data(arrySummariesByHour), Name = "Missions Per Hour"},
            new Series {Data = new Data(arrySummariesByHalfHour), Name = "Mission per Half Hour"}
        });

So my question.. is how to get the blue line to extend across the chart and the points be lined up with 0, 1, 2, 3, etc.. and not 0, 0.5, 1, 1.5..etc.. 所以我的问题..是如何使蓝线延伸到整个图表上,并且将点与0、1、2、3等对齐,而不是与0、0.5、1、1.5 ..等对齐。

You're trying to plot two series with an x-axis that contains categories, but one of your series doesn't have as many points as there are categories (you have half the needed points). 您试图绘制两个带有包含类别的x轴的系列,但是其中一个系列的点数不及类别(所需点数的一半)。

To fix your issues, you should add null values to your hour series, at each location that corresponds to the half hour categories, so that both series have the same number of points. 要解决您的问题,您应该在与半小时类别相对应的每个位置的小时序列中添加null值,以使两个序列的分数相同。 Then, you choose to connect the null values so that the line is solid. 然后,您选择连接null值,以使线是实线。

Here's a example to see what I mean (it's in pure javascript, but the concepts are the same): 这是一个示例,以了解我的意思(它使用的是纯JavaScript,但概念相同):

 Highcharts.chart('container', { xAxis: { categories: ['0.5', '1', '1.5', '2', '2.5', '3', '3.5', '4', '4.5', '5', '5.5', '6'] }, plotOptions: { series: { connectNulls: true } }, series: [{ data: [2, 7, 10, 12, 14, 17, 13, 14, 21, 19, 9, 5], }, { data: [1, 5, 9, 2, 4, 7], }, { data: [1, null, 5, null, 9, null, 2, null, 4, null, 7], }] }); 
 <script src="https://code.highcharts.com/highcharts.js"></script> <div id="container" style="height: 400px"></div> 

The first series is your half-hour serie, the second is your current hour serie, and the third is your new one. 第一个系列是您的半小时系列,第二个是您的当前小时系列,第三个是您的新系列。

Now, in C#, you'd have: 现在,在C#中,您将拥有:

var arrySummariesByHour = lstGroupSummariesByHour
    .SelectMany(x => new object[] {
        x.Count(), // hour tick
        null       // half hour tick
    }).ToArray();

and the options to join null values: 以及连接空值的选项:

.SetPlotOptions(new PlotOptions
{
     Series = new PlotOptionsSeries 
     {
         ConnectNulls = true
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM