如何使用主题文件自定义ASP.NET图表 [英] How To Customize ASP.NET Chart Using A Theme File

查看:81
本文介绍了如何使用主题文件自定义ASP.NET图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个折线图,在x轴上是日期,在y轴上是绿色(位置0),黄色(1)和红色(2).

我该如何实现?

目前只有数字. 我用XML进行了尝试,但是我对其了解不多,有点混乱.我可以以此访问y轴的单个元素并将其转换为文本吗? 我可以以某种方式在axisLabel的Chart.AddSeries方法内实现if else方法吗?

控制器

//XML
string t = @"
<Chart> 
  <ChartAreas> 
    <ChartArea Name=""Default"" _Template_=""All""> 
      <AxisY Interval=""1""> 
        <LabelStyle Font=""Verdana, 70px"" /> 
      </AxisY> 
    </ChartArea> 
  </ChartAreas> 
</Chart>";

var Date_min = OpenDate;
var Date_max = DateTime.Today;

var chart = new Chart(width: 300, height: 200, theme: t)
           .AddSeries(
                      chartType: "line",
                      name: "Temperature",
                      markerStep: 2,
                      xValue: Date_X,
                      yValues: Temperature_Y)       //0,1 or 2 for green, yellow and red                     
           .SetXAxis("Date", Date_min.ToOADate(), Date_max.ToOADate())
           .SetYAxis("Temperature", 0, 2.5)
           .GetBytes("png");

谢谢您的帮助.

更新

我浏览了System.Web.UI.DataVisualization.Charting框架,但没有找到任何其他函数来解决我的问题.

我想更改y轴标签.不是y轴的常规标签,而是它的每个位置. y轴上只有三个位置必须重命名为绿色,黄色和红色,而不是0,1,2. x轴的每个日期将具有相应的颜色.

解决方案

使用以下主题文件(或字符串)创建自定义轴标签:

<?xml version="1.0" encoding="utf-8" ?> 
 <Chart> 
  <ChartAreas> 
    <ChartArea Name="Default" _Template_="All"> 
      <AxisY>
        <CustomLabels>
          <CustomLabel Text="GREEN (0 - 1)" ToPosition="1" />
          <CustomLabel FromPosition="1" Text="YELLOW (1 - 2)" ToPosition="2" />
          <CustomLabel FromPosition="2" Text="RED (2 - 3)" ToPosition="3" />
        </CustomLabels>
      </AxisY> 
    </ChartArea>
  </ChartAreas>
   <Series>
     <Series Name="Temperature" BorderWidth="3" >
     </Series>
   </Series>
   <Legends>
     <Legend Alignment="Center" Docking="Top" Name="Temperature">
     </Legend>
   </Legends>
 </Chart>

Controller.cs:

var Date_min = DateTime.Now.AddDays(-4);
var Date_max = DateTime.Now.AddDays(1);

        var chart = new Chart(width: 600, height: 400, themePath: "XMLFile1.xml")
                   .AddSeries(
                              chartType: "line",
                              name: "Temperature",
                              xValue: new DateTime[] { DateTime.Now.AddDays(-4), DateTime.Now.AddDays(-3), DateTime.Now.AddDays(-2), DateTime.Now.AddDays(-1), DateTime.Now },
                              yValues: new int[] { 2, 1, 2, 2, 1 })       //0,1 or 2 for green, yellow and red                     
                   .SetXAxis("Date", Date_min.ToOADate(), Date_max.ToOADate())
                   .SetYAxis("Temperature", 0, 3.0)
                   .Save("~/Image/MyChart.png", "png");

i want to create a line Chart, where at the x-axis are the dates and on the y-axis there are the positions green(at the position of 0), yellow (1) and red (2).

How can i achieve this?

At the moment there are just the numbers. I tried it with XML, but i dont know much about it and its a little bit confusing. Can i access the single elements of the y-axis with this and convert them to text? Can i somehow implement an if else method within the Chart.AddSeries Method at axisLabel?

Controller

//XML
string t = @"
<Chart> 
  <ChartAreas> 
    <ChartArea Name=""Default"" _Template_=""All""> 
      <AxisY Interval=""1""> 
        <LabelStyle Font=""Verdana, 70px"" /> 
      </AxisY> 
    </ChartArea> 
  </ChartAreas> 
</Chart>";

var Date_min = OpenDate;
var Date_max = DateTime.Today;

var chart = new Chart(width: 300, height: 200, theme: t)
           .AddSeries(
                      chartType: "line",
                      name: "Temperature",
                      markerStep: 2,
                      xValue: Date_X,
                      yValues: Temperature_Y)       //0,1 or 2 for green, yellow and red                     
           .SetXAxis("Date", Date_min.ToOADate(), Date_max.ToOADate())
           .SetYAxis("Temperature", 0, 2.5)
           .GetBytes("png");

Thank you for your help in advance.

Update

I looked through the System.Web.UI.DataVisualization.Charting Framework and did not find any additional function to solve my Problem.

I want to change the y-axis labels. Not the general Label of the y-axis but each position of it. There will be just three positions of the y-axis that have to be renamed to green, yellow and red instead of 0,1,2. Each Date of the x-axis will have a corresponding color.

解决方案

Use this theme file (or string) to create a custom axis label:

<?xml version="1.0" encoding="utf-8" ?> 
 <Chart> 
  <ChartAreas> 
    <ChartArea Name="Default" _Template_="All"> 
      <AxisY>
        <CustomLabels>
          <CustomLabel Text="GREEN (0 - 1)" ToPosition="1" />
          <CustomLabel FromPosition="1" Text="YELLOW (1 - 2)" ToPosition="2" />
          <CustomLabel FromPosition="2" Text="RED (2 - 3)" ToPosition="3" />
        </CustomLabels>
      </AxisY> 
    </ChartArea>
  </ChartAreas>
   <Series>
     <Series Name="Temperature" BorderWidth="3" >
     </Series>
   </Series>
   <Legends>
     <Legend Alignment="Center" Docking="Top" Name="Temperature">
     </Legend>
   </Legends>
 </Chart>

Controller.cs:

var Date_min = DateTime.Now.AddDays(-4);
var Date_max = DateTime.Now.AddDays(1);

        var chart = new Chart(width: 600, height: 400, themePath: "XMLFile1.xml")
                   .AddSeries(
                              chartType: "line",
                              name: "Temperature",
                              xValue: new DateTime[] { DateTime.Now.AddDays(-4), DateTime.Now.AddDays(-3), DateTime.Now.AddDays(-2), DateTime.Now.AddDays(-1), DateTime.Now },
                              yValues: new int[] { 2, 1, 2, 2, 1 })       //0,1 or 2 for green, yellow and red                     
                   .SetXAxis("Date", Date_min.ToOADate(), Date_max.ToOADate())
                   .SetYAxis("Temperature", 0, 3.0)
                   .Save("~/Image/MyChart.png", "png");

这篇关于如何使用主题文件自定义ASP.NET图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆