问题
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.
回答1:
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");
来源:https://stackoverflow.com/questions/38169328/how-to-customize-asp-net-chart-using-a-theme-file