如何在ms-chart控件中获取条形颜色 [英] How to get Bar color in ms-chart control

查看:76
本文介绍了如何在ms-chart控件中获取条形颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在使用ms-chart控件绘制图表.
我将数据分配给图形为

Hi All,

I am using ms-chart control to draw a chart.
I am assigning the data to the graph as

commonChart1.DataSource = dataTableToBind;
commonChart1.DataBind();


还可以使用该系列的调色板来绘制条形图.

我的图表类型是列.
我想在图例中添加图例,


Also using the Palette for the series to draw the bars.

My chart type is column.
I want to add legends to this which I got from the following code

commonChart1.Legends.Add("Default")
For Each row As DataRow In dtToBind.Rows
Try
  Dim legendItem As LegendItem = New LegendItem()
  legendItem.Name = checkDBNull(row(0)).ToString()
  commonChart1.Legends("Default").CustomItems.Add(legendItem)
                   
  Catch ex As Exception

End Try



现在,图表显示图例,但所有图例的背景颜色均为白色.
我想为图例颜色分配特定的系列点颜色.

我该如何实现?



Now the Chart is displaying the legends, but all legends have the back colour white.
I want to assign the legend colour with the particular series point colour.

How can I achieve this?

推荐答案

在.aspx文件中添加图例.图表控件将注意图例的条形颜色.


Add legends in your .aspx file. The charting control will take care of bar color for legends.


<asp:chart id="Chart1" runat="server" BackColor="OrangeRed" Width="600px"

                      Height="300px" BorderColor="26, 59, 105" Palette="BrightPastel"

                      BorderDashStyle="Solid" BackSecondaryColor="White"

                      BackGradientStyle="TopBottom" BorderWidth="2"

                      ImageLocation="~/TempImages/ChartPic_#SEQ(300,3)"

                      BackImageTransparentColor="White">
                          <titles>
                              <asp:Title ShadowColor="32, 0, 0, 0" Font="Trebuchet MS, 14.25pt, style=Bold" ShadowOffset="3" Text="Attendance" ForeColor="26, 59, 105"></asp:Title>
                          </titles>
                          <legends>
                              <asp:Legend Enabled="True" IsTextAutoFit="False" Name="Default" BackColor="Transparent" Font="Trebuchet MS, 8.25pt, style=Bold" Docking="Bottom"></asp:Legend>
                          </legends>
                          <borderskin SkinStyle="Emboss"></borderskin>
                          <series>
                              <asp:Series IsValueShownAsLabel="True" ChartArea="ChartArea1" Name="Default" CustomProperties="LabelStyle=Bottom" BorderColor="180, 26, 59, 105" LabelFormat="#.#,H"></asp:Series>
                              <asp:Series IsValueShownAsLabel="True" ChartArea="ChartArea1" Name="Default1" CustomProperties="LabelStyle=Bottom" BorderColor="180, 26, 59, 105" LabelFormat="#.#,H"></asp:Series>
                              <asp:Series IsValueShownAsLabel="True" ChartArea="ChartArea1" Name="Default2" CustomProperties="LabelStyle=Bottom" BorderColor="180, 26, 59, 105" LabelFormat="#.#,H"></asp:Series>

                          </series>
                          <chartareas>
                              <asp:ChartArea Name="ChartArea1" BorderColor="64, 64, 64, 64" BorderDashStyle="Solid" BackSecondaryColor="White" BackColor="64, 165, 191, 228" ShadowColor="Transparent" BackGradientStyle="TopBottom">
                                  <axisy2 Enabled="False"></axisy2>
                                  <axisx2 Enabled="False"></axisx2>
                                  <area3dstyle Rotation="10" Perspective="10" Inclination="15" IsRightAngleAxes="False" WallWidth="0" IsClustered="False" />
                                  <axisy LineColor="64, 64, 64, 64" IsLabelAutoFit="False" ArrowStyle="Triangle">
                                      <LabelStyle Font="Trebuchet MS, 8.25pt, style=Bold" Format="#.#,H" />
                                      <MajorGrid LineColor="64, 64, 64, 64" />
                                  </axisy>
                                  <axisx LineColor="64, 64, 64, 64" IsLabelAutoFit="False" ArrowStyle="Triangle">
                                      <LabelStyle Font="Trebuchet MS, 8.25pt, style=Bold" IsStaggered="false" />
                                      <MajorGrid LineColor="64, 64, 64, 64" />
                                  </axisx>
                              </asp:ChartArea>
                          </chartareas>
                      </asp:chart>





请注意上述示例中的





notice in the above example

<legends>
                              <asp:Legend Enabled="True" IsTextAutoFit="False" Name="Default" BackColor="Transparent" Font="Trebuchet MS, 8.25pt, style=Bold" Docking="Bottom"></asp:Legend>
                          </legends>


尝试以下方式:



try the following way:



String connString = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ToString();
 String sSQL = "select distinct subject, marks FROM tblStudentMarks order by 2";
 DataTable result = new DataTable();
 using (SqlConnection conn = new SqlConnection(connString))
 {
  using (SqlCommand cmd = new SqlCommand())
  {
   conn.Open();
   cmd.CommandText = sSQL;
   cmd.Connection = conn;
   SqlDataReader dr;
   dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
   result.Load(dr);
   dr = null;
  }
 }
 String sSeries = "";
 //Assaigning ChartArea Name
 Chart2.ChartAreas.Add("chtArea");
 //Binding to Legends
 Chart2.Legends.Add("chtArea");
 LegendItem legendItem = new LegendItem();
 for (int i = 0; i < result.Rows.Count; i++)
 {
  sSeries = result.Rows[i][0].ToString().ToUpper();
  if(Chart2.Series.FindByName(sSeries) == null)
  {
   //Creating Unique Series
   Chart2.Series.Add(sSeries);
   legendItem.Name = sSeries;
   //Below some properties... of Legends
   legendItem.BorderWidth = 4;
   legendItem.ShadowOffset = 1; 
   legendItem.Color = Color.FromName(Chart2.Series[sSeries].Color.Name.ToString());
   legendItem = new LegendItem();
  }
  Chart2.Series[sSeries].ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Column;
  //Chart2.Series[sSeries].ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Bar;

  //Binding UserCounts
  Chart2.Series[sSeries].Points.AddY(Convert.ToDouble(result.Rows[i][1].ToString()));

  //Series properties
  Chart2.Series[sSeries].IsVisibleInLegend = true;
  Chart2.Series[sSeries].IsValueShownAsLabel = true;
  Chart2.Series[sSeries].ToolTip = "Data Point Y Value: #VALY{G}";
 }
 result = null;

 //Axis properties
 Chart2.ChartAreas[0].AxisX.Title = "User Roles";
 Chart2.ChartAreas[0].AxisX.TitleFont = new System.Drawing.Font("Verdana", 10, System.Drawing.FontStyle.Bold);
 Chart2.ChartAreas[0].AxisY.Title = "User Count";
 Chart2.ChartAreas[0].AxisY.TitleFont = new System.Drawing.Font("Verdana", 10, System.Drawing.FontStyle.Bold);
 Chart2.ChartAreas[0].AxisY2.LineColor = Color.Black;
 Chart2.ChartAreas[0].BorderDashStyle = ChartDashStyle.Solid;
 Chart2.ChartAreas[0].BorderWidth = 1;
 Chart2.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
 Chart2.BorderlineColor = System.Drawing.Color.FromArgb(26, 59, 105);
 Chart2.BorderlineWidth = 2;
 Chart2.BackColor = Color.AliceBlue;

 //Legends properties
 for (int j = 0; j < Chart2.Legends.Count; j++)
 {
  Chart2.Legends[j].BorderColor = Color.Black;
  Chart2.Legends[j].BorderWidth = 1;
  Chart2.Legends[j].BorderDashStyle = ChartDashStyle.Solid;
  Chart2.Legends[j].ShadowOffset = 1;
  Chart2.Legends[j].LegendStyle = LegendStyle.Table;
  Chart2.Legends[j].TableStyle = LegendTableStyle.Auto;
  Chart2.Legends[j].Docking = Docking.Bottom;
  Chart2.Legends[j].Alignment = StringAlignment.Center;
  Chart2.Legends[j].Enabled = true;
  Chart2.Legends[j].Font = new System.Drawing.Font("Verdana", 8, System.Drawing.FontStyle.Bold);
  Chart2.Legends[j].AutoFitMinFontSize = 5;
 }


图表图例会根据系列中使用的颜色自动选择颜色.您只能为自定义图例项目指定颜色

Chart legend automatically selects color based on the color used in the series. You can only specify color for custom legend item

chart1.Series[0].Color = Color.Green;



尝试将您的问题放在MSChart论坛中,此处 [



try to put your question in MSChart forum, Here[^].


这篇关于如何在ms-chart控件中获取条形颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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