简体   繁体   English

使用System.Web.UI.DataVisualization.Charting命名空间更改饼图上的标签颜色

[英]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 . 因此,我下一步ChartAreasChartAreas之前添加系列开放标签和系列结束标签。 In between teh series tag I added another series tag that had the Name and LabelForeColor properties like so: 在这两个系列标签之间,我添加了另一个具有NameLabelForeColor属性的系列标签,如下所示:

<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.

相关问题 找不到命名空间System.Web.UI.DataVisualization.Charting - Cannot find namespace System.Web.UI.DataVisualization.Charting System.Web.UI.DataVisualization.Charting中的雷达图选择性标签旋转? - Radar chart selective label rotation in System.Web.UI.DataVisualization.Charting? 有没有一种方法可以使用System.Web.UI.DataVisualization.Charting旋转烛台图表以水平显示,而不是垂直显示? - Is there a way using System.Web.UI.DataVisualization.Charting to rotate a Candlestick chart to display horizontally instead of vertically? IE8上未显示System.Web.UI.DataVisualization.Charting图表 - System.Web.UI.DataVisualization.Charting charts are not displaying on IE8 System.Web.UI.DataVisualization.Charting .net 4.0 中缺少错误? - System.Web.UI.DataVisualization.Charting missing error in .net 4.0? iTextSharp将System.Web.UI.DataVisualization.Charting转换为pdf - iTextSharp convert a System.Web.UI.DataVisualization.Charting to pdf 如何使用System.Web.UI.DataVisualization.Charting - How to work with System.Web.UI.DataVisualization.Charting 如何在 .NET Core 应用程序中使用 System.Web.UI.DataVisualization.Charting? - How do I use System.Web.UI.DataVisualization.Charting in .NET Core apps? 如何将System.Web.UI.DataVisualization.Charting dll放入WebMatrix 3? - How do I get System.Web.UI.DataVisualization.Charting dll into WebMatrix 3? Visual Studio 2010与.NET 4.0无法识别System.Web.UI.DataVisualization.Charting - Visual Studio 2010 with .NET 4.0 not recognising System.Web.UI.DataVisualization.Charting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM