[英]Changing the label color on a Pie Chart using System.Web.UI.DataVisualization.Charting namespace
I am trying to use the System.Web.UI.DataVisualization.Charting namespace to generate a pie chart and I got it to do mostly what I want. 我正在尝试使用System.Web.UI.DataVisualization.Charting命名空间来生成饼形图,而我得到的大部分是我想要做的。 The Theme I'm using has the following code:
我正在使用的主题具有以下代码:
public static class ChartThemes
{
public const string Blue =
@"<Chart ForeColor=""#ffffff"" BackColor=""#0000ff"" BackGradientStyle=""TopBottom"" BackSecondaryColor=""White"" BorderColor=""26, 59, 105"" BorderlineDashStyle=""Solid"" BorderWidth=""2"" Palette=""none"" PaletteCustomColors=""#0000FF; #0055ff; #5599ff;"">
<Titles>
<Title ForeColor=""#ffffff"" Name=""main"" Font=""Arial Black, 16pt, style=Bold"" />
</Titles>
<ChartAreas>
<ChartArea Name=""Default"" _Template_=""All"" BackColor=""64, 165, 191, 228"" BackGradientStyle=""TopBottom"" BackSecondaryColor=""White"" BorderColor=""64, 64, 64, 64"" BorderDashStyle=""Solid"" ShadowColor=""Transparent"">
<AxisY LineColor=""64, 64, 64, 64"" TitleForeColor=""#ffffff"" TitleFont=""Arial Black, 8pt"">
<MajorGrid Interval=""Auto"" LineColor=""#00ff00"" />
<LabelStyle Font=""Trebuchet MS, 16pt, style=Bold"" ForeColor=""#ffffff"" />
</AxisY>
<AxisX TitleForeColor=""#ffffff"" TitleFont=""Arial Black, 8pt"">
<LabelStyle ForeColor=""#ffffff"" />
<MajorGrid Interval=""Auto"" LineColor=""#ff0000"" />
</AxisX>
<AxisX2>
<LabelStyle ForeColor=""#ffffff"" />
</AxisX2>
<AxisY2>
<LabelStyle ForeColor=""#ffffff"" />
</AxisY2>
</ChartArea>
</ChartAreas>
<Legends>
<Legend _Template_=""All"" BackColor=""Transparent"" Font=""Trebuchet MS, 8.25pt, style=Bold"" IsTextAutoFit=""False"" ForeColor=""#ffffff""/>
</Legends>
<BorderSkin SkinStyle=""Emboss"" />
</Chart>";
}
Then I'm generating my pie chart like this: 然后,我将生成如下的饼图:
public ActionResult PieChart()
{
var chart2 = new Chart(width: 600, height: 600, theme: Helpers.ChartThemes.Blue).AddTitle("Pie Chart","main")
.AddSeries("Default", chartType: "Pie", xValue: new[] { "Pizza", "Hamburgers", "Hot Dogs" }, yValues: new[] { 20, 20, 60 })
.AddLegend()
.GetBytes("png");
return File(chart2, "image/png");
}
I then get a chart that looks like: 然后,我得到一个如下图的图表:
The problem is that I don't want the labels that appear on the pie segments themselves to be black, I want them to be white. 问题是我不希望出现在饼图段本身上的标签是黑色的,我希望它们是白色的。 However, I cannot find much documentation on how to do it in XML or any other way.
但是,我找不到太多有关如何以XML或其他方式进行操作的文档。
So my question is how to do I use the XML to change the color of the labels on the segments of the pie itself (the ones that appear black in the picture)? 因此,我的问题是如何使用XML更改饼图本身的片段(图片中显示为黑色的片段)上标签的颜色?
Alright, I figured it out. 好吧,我知道了。 What I had to do was add a series to the XML.
我要做的是向XML添加一系列。 To do this I had to know the name of the series and I know that because when I called the
AddSeries
method on chart2
the first parameter is its name and therefore my series name is Default
. 为此,我必须知道系列的名称,而且我知道,因为当我在
chart2
上调用AddSeries
方法时,第一个参数是它的名称,因此我的系列名称是Default
。
So the next step I took was to add a series open tag and a series end tag before ChartAreas
. 因此,我下一步
ChartAreas
在ChartAreas
之前添加系列开放标签和系列结束标签。 In between teh series tag I added another series tag that had the Name
and LabelForeColor
properties like so: 在这两个系列标签之间,我添加了另一个具有
Name
和LabelForeColor
属性的系列标签,如下所示:
<Series Name=""Default"" LabelForeColor=""#ffffff"" />
If you wanted to add more properties you could by using the properties listed in this Documentation. 如果要添加更多属性,可以使用本文档中列出的属性。
So the end XML looked like: 因此,最终的XML看起来像:
@"<Chart ForeColor=""#ffffff"" BackColor=""#0000ff"" BackGradientStyle=""TopBottom"" BackSecondaryColor=""White"" BorderColor=""26, 59, 105"" BorderlineDashStyle=""Solid"" BorderWidth=""2"" Palette=""none"" PaletteCustomColors=""#0000FF; #0055ff; #5599ff;"">
<Titles>
<Title ForeColor=""#ffffff"" Name=""main"" Font=""Arial Black, 16pt, style=Bold"" />
</Titles>
<Series>
<Series Name=""Default"" LabelForeColor=""#ffffff"" />
</Series>
<ChartAreas>
//... and so on and so forth
This was the resulting image: 这是生成的图像:
I figured this out with help from This blog post . 我在本博客文章的帮助下弄清楚了这一点。 I noticed that they were changing the series color so I figured something similar in the XML would do the trick.
我注意到他们正在更改系列颜色,因此我发现XML中的类似内容可以解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.