ASP.Net中的图表助手显示DateTime

时间:2016-12-15 22:18:52

标签: c# asp.net .net asp.net-mvc charts

在asp.net mvc应用程序中,我需要创建一个简单的折线图,所以我尝试了图表助手。使用两个列表创建图表,x轴应显示日期时间,y轴应显示每次的值。现在使用下面的代码可以正常工作。 " listOfDateTimes"包含DateTimes列表。

    var chart = new Chart(width: 1000, height: 300, theme: ChartTheme.Green)
        .AddSeries(
                    chartType: "line",
                    xValue: listOfDateTimes,
                    yValues:  listOfValues )
                    .GetBytes("png");

问题是x轴下方的日期时间显示为" MM / dd / yyyy",但我还需要显示小时和分钟。我无法弄清楚如何使用Chart帮助器来解决这个问题。

1 个答案:

答案 0 :(得分:3)

使用Chart Helper时,您必须通过提供自己的主题文件来自定义图表:

 var myChart = new Chart(width: 800, height: 400, themePath: "MyTheme3.xml")
              .AddSeries(
                          chartType: "line",
                          xValue: new[] { DateTime.Now,
                              DateTime.Now.AddSeconds(1),
                              DateTime.Now.AddSeconds(2),
                              DateTime.Now.AddSeconds(3),
                              DateTime.Now.AddSeconds(4) },
                          yValues: new[] { 40, 100, 60, 80, 20 })

使用此示例主题按照描述自定义X轴:

<强> MyTheme3.xml:

<?xml version="1.0" encoding="utf-8" ?>
<Chart>
  <ChartAreas>
    <ChartArea Name="Default">
      <AxisX>
        <LabelStyle Format="MM/dd/yyyy hh:mm:ss"></LabelStyle>
      </AxisX>
    </ChartArea>
  </ChartAreas>
  <Series>
    <Series Name="Default"></Series>
  </Series>
</Chart>

enter image description here

编辑:如果要以常规方式配置图表,则应使用System.Web.UI.DataVisualization.Charting命名空间。但是如果你想使用System.Web.Helpers命名空间,那么你必须像之前解释的那样去做。