如何设置饼图切片的颜色? asp.net mvc

时间:2015-08-13 17:56:20

标签: c# asp.net-mvc-5

如何使用图表类设置每个饼图切片的颜色?

根据我的阅读,我觉得我需要修改我的主题,但我不知道如何。

这是我到目前为止所拥有的。

 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");
    }

1 个答案:

答案 0 :(得分:0)

public static OleDbConnection GetConnection() { try { if (conn.State == ConnectionState.Closed) { System.Configuration.ConnectionStringSettingsCollection Constrings = System.Configuration.ConfigurationManager.ConnectionStrings; System.Configuration.ConnectionStringSettings Constring = Constrings["Webdav"]; conn.ConnectionString = Constring.ConnectionString; conn.Open(); } return conn; } catch (Exception ex) { throw ex; } } public static OleDbDataReader GetData(string texto, CommandType tipo) { var comando = new OleDbCommand { Connection = GetConnection(), CommandText = texto, CommandType = tipo, }; return comando.ExecuteReader(); } public static DbFile GetUniqueFile(string urlPath) { ... var data = GetData(....); if (data.HasRows) { data.Read(); var file = DbFile.GetObject(data); data.Close(); return file; } return null; } 类允许您使用ChartThemes中的一个。但每个主题只会为您提供一组预定义的颜色。对于饼图的特定切片,您不能像不同颜色那样进行自定义。

您可以尝试使用某些javascript图表库,例如Chart.jsHighcharts,它们可让您根据需要自定义颜色。

编辑:如果需要,您可以创建自定义主题。它基本上是这样的XML结构的字符串版本。

Chart

因此,要开始,您可以创建一个具有此字符串

的类
<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>

并使用它。

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>";
}

您可以将xml保存在一个真实的xml文件中,并让一些C#代码读取该文件的内容并返回其字符串化版本,而不是硬编码该类中的大型xml字符串。