asp.net c# how to use custom colours in charts
can any one show me I might be able to use custom colours in an ASP.Net chart control. so for example I have two series TOTAL and TARGET and I want to set the colour for total to 开发者_StackOverflow社区green and the colour for target to be red .
Something like this:
Color[] colors = new Color[] { Color.Green, Color.Red };
foreach (Series series in Chart1.Series)
{
foreach (DataPoint point in series.Points)
{
point.LabelBackColor = colors[series.Points.IndexOf(point)];
}
}
iv done something like
Chart2.Series.Add(new Series("Target")
{
ChartType = SeriesChartType.Column,
Color = Color.Red,
});
suppose that you have two points for your Series1. So you can customise the colors as follow:
Chart1.Series["Series1"].Points[0].Color=Color.Red;
Chart1.Series["Series1"].Points[1].Color=Color.Yellow;
The System.Web.UI.DataVisualization.Charting.Series
object has a Color
property. Simply set the color property to what you need it to be. Here follows a code sample that adds a horizontal line of a given value to a chart:
public static System.Web.UI.DataVisualization.Charting.Chart addLineToChart(
System.Web.UI.DataVisualization.Charting.Chart pChart, double pValue, System.Drawing.Color pColor)
{
// I will declare a new series where every value is the value passed in
System.Web.UI.DataVisualization.Charting.Series constantLineSeries = new System.Web.UI.DataVisualization.Charting.Series();
constantLineSeries.ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Line;
constantLineSeries.BorderWidth = ChartingValues.CHART_LINE_THICKNESS;
constantLineSeries.Color = pColor;
// At each point in the old series, add a constant point in the new series.
foreach(System.Web.UI.DataVisualization.Charting.DataPoint point in pChart.Series[0].Points)
{
System.Web.UI.DataVisualization.Charting.DataPoint constantLinePoint = new System.Web.UI.DataVisualization.Charting.DataPoint();
constantLinePoint.XValue = point.XValue;
constantLinePoint.YValues = new double[] { pValue };
constantLineSeries.Points.Add(constantLinePoint);
}
pChart.Series.Add(constantLineSeries);
pChart.ChartAreas[0].Area3DStyle.Enable3D = false;
return pChart;
}
This code sample makes adjustments to some other properties you will likely find useful as well.
I don't know if this will help, but I created a Custom Control that exposes several parameters that you can adjust, such as chart type, color, wall width, etc.
http://www.foliotek.com/devblog/asp-net-4-0-custom-chart-control-adjustable/
精彩评论