如何在Asp.Net C#Chart中区分每个条形图中的颜色?

时间:2017-10-31 05:17:02

标签: c# asp.net graph charts

我在asp.net c#中开发web应用程序,在其中我使用图表(条形图),在这个条形图中所有条形图都是相同颜色,但我想在每个条形图中显示自定义颜色,但我不知道不知道如何显示,任何人都让我知道代码如何区分。

现在下面的图表显示所有蓝色。

但我希望每个条形图都是单独的颜色。

enter image description here

ASP.NET

<asp:Chart ID="Chart2" runat="server" Width="450px" Height="350px">
<Series>
    <asp:Series Name="Series1" ChartArea="ChartArea1">
 </asp:Series>
</Series>
<ChartAreas>
   <asp:ChartArea Name="ChartArea1">
     <Area3DStyle WallWidth="1" LightStyle="Realistic"></Area3DStyle>
   </asp:ChartArea>
 </ChartAreas>
</asp:Chart>

C#

Chart2.DataSource = ds2.Tables[0];

Chart2.Series["Series1"].XValueMember = "Days";
Chart2.Series["Series1"].YValueMembers = "Count";

Chart2.ChartAreas["ChartArea1"].AxisX.MajorGrid.Enabled = false;
Chart2.ChartAreas["ChartArea1"].AxisX.MinorGrid.Enabled = false;
Chart2.ChartAreas["ChartArea1"].AxisY.MajorGrid.Enabled = false;
Chart2.ChartAreas["ChartArea1"].AxisY.MinorGrid.Enabled = false;
Chart2.ChartAreas[0].AxisX.Interval = 1;
Chart2.Series["Series1"].Color = System.Drawing.ColorTranslator.FromHtml("#418cf0");
Chart2.Series["Series1"].ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.StackedColumn;
Chart2.Series["Series1"].IsValueShownAsLabel = true;
Chart2.DataBind();

1 个答案:

答案 0 :(得分:0)

最后我找到了答案

https://stackoverflow.com/a/6440711/5772054

Color[] colors = new Color[] { Color.Green, Color.LightGreen, Color.YellowGreen, Color.Yellow, Color.Maroon, Color.Red };
foreach (Series series in Chart2.Series)
  {
    foreach (DataPoint point in series.Points)
     {
       //point.LabelBackColor = colors[series.Points.IndexOf(point)];
       point.Color = colors[series.Points.IndexOf(point)];
     }
  }