简体   繁体   English

如何设置饼图切片的颜色? ASP.NET MVC

[英]How to set Pie Chart slices' colors? asp.net mvc

How can I set the colors of each pie slice using the Chart Class? 如何使用“图表类”设置每个饼图的颜色?

From what I have read I feel like i need to modify my theme, but I don't know how. 通过阅读,我觉得我需要修改主题,但是我不知道该怎么做。

This is what i have so far. 这是我到目前为止所拥有的。

 public ActionResult Q5Chart()
    {
        int strAgr = selected.Where(x => x.q5 == 5).Count();
        int agr = selected.Where(x => x.q5 == 4).Count();
        int neu = selected.Where(x => x.q5 == 3).Count();
        int dis = selected.Where(x => x.q5 == 2).Count();
        int strDis = selected.Where(x => x.q5 == 1).Count();

        string myTheme = @"<Chart>
                                <Series>
                                    <Series Name=""Question 5"" ChartType=""Pie"" CustomProperties=""PieLabelStyle=Disabled"">
                                    </Series>
                                </Series>
                            </Chart>";

        var Q5Chart = new Chart(width: 450, height: 300, theme: myTheme)
        .AddSeries(
        chartType: "Pie",
        name: "Question 5",
        xValue: new[] { "Strongly Agree", "Agree", "Neutral", "Strongly Disagree", "Disagree" },
        yValues: new[] { strAgr,agr,neu,dis,strDis }).AddLegend();


        return File(Q5Chart.ToWebImage().GetBytes(), "image/jpeg");
    }

Chart class allows you to use one of the ChartThemes available. Chart类允许您使用可用的ChartThemes之一。 But each theme will give you only the predefined set of colors. 但是每个主题只会为您提供预定义的颜色集。 You can't do customization like different colors for specific slices of your pie charts. 您无法为饼图的特定部分进行自定义,例如不同的颜色。

You can try using some javascript charting libraries like Chart.js or Highcharts and they will let you customize colors as you want. 您可以尝试使用一些JavaScript图表库,例如Chart.jsHighcharts ,它们可以让您根据需要自定义颜色。

EDIT : If you want, you can create a custom theme. 编辑:如果需要,您可以创建一个自定义主题。 It is basically a string version of an XML structure like this. 它基本上是这样的XML结构的字符串版本。

<Chart BackColor="#D3DFF0" BackGradientStyle="TopBottom" BackSecondaryColor="White" BorderColor="26, 59, 105" BorderlineDashStyle="Solid" BorderWidth="2" Palette="BrightPastel">
  <ChartAreas>
    <ChartArea Name="Default" _Template_="All" BackColor="64, 165, 191, 228" BackGradientStyle="TopBottom" BackSecondaryColor="White" BorderColor="64, 64, 64, 64" BorderDashStyle="Solid" ShadowColor="Transparent" />
  </ChartAreas>
  <Legends>
  <Legend _Template_="All" BackColor="Transparent" Font="Trebuchet MS, 8.25pt, style=Bold" IsTextAutoFit="False" /><BorderSkin SkinStyle="Emboss" />
</Chart>

So to get started, you can create a class which has this string 因此,开始时,您可以创建一个包含此字符串的类

public static class MyChartTheme
{
    public const string MyCustom = "<Chart BackColor=\"White\" BackGradientStyle=\"TopBottom\" BackSecondaryColor=\"White\" BorderColor=\"26, 59, 105\" BorderlineDashStyle=\"Solid\" BorderWidth=\"2\" Palette=\"BrightPastel\">\r\n    <ChartAreas>\r\n        <ChartArea Name=\"Default\" _Template_=\"All\" BackColor=\"64, 165, 191, 228\" BackGradientStyle=\"TopBottom\" BackSecondaryColor=\"White\" BorderColor=\"64, 64, 64, 64\" BorderDashStyle=\"Solid\" ShadowColor=\"Transparent\" /> \r\n    </ChartAreas>\r\n    <Legends>\r\n        <Legend _Template_=\"All\" BackColor=\"Transparent\" Font=\"Trebuchet MS, 8.25pt, style=Bold\" IsTextAutoFit=\"False\" /> \r\n    </Legends>\r\n    <BorderSkin SkinStyle=\"Emboss\" /> \r\n  </Chart>";
}

and use it. 并使用它。

var chart= new Chart(width: 600, height: 400, theme: MyChartTheme.MyCustom)

You can keep the xml in a real xml file and have some C# code read the content of this file and return a stringified version of that instead of hard coding the big xml string in the class. 您可以将xml保留在真实的xml文件中,并让一些C#代码读取该文件的内容并返回该文件的字符串化版本,而不是对类中的大型xml字符串进行硬编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM