开发者

How to change the default colors of the pie chart when using Chart helper in ASP.NET MVC

Is it possible to specify what color each section of the pie lo开发者_运维知识库oks like? Also, is it possible to display the legend on the side?

For ex: in the chart below, can I some specify that , “Pass“ shows up as green, Fail as red and unknown as grey?

    public ActionResult GetRainfallChart()
    {

        var key = new Chart(width: 600, height: 400).AddSeries(
                               chartType: "pie",
                               legend: "Test pass",
                               xValue: new[] { "Pass", "Fail", "Unknown" },
                               yValues: new[] { "50", "30", "20" })
                               .Write();

        return null;
    }

If its not possible with the "chart" control that comes in System.Web.Helpers assembly, then can someone point me how to do this with asp.net control for webforms in a MVC project


You can set the color on the series. There's a Color property.

For example:

// Set color for the whole series
Chart.Series[0].Color = Color.Green;

// Set color of a single data point
Chart.Series[0].Points[5].Color = Color.Red;

For more info on colours, see this blog entry: Setting Microsoft Chart Series Colors


Unfortunately, Chart doesnt have a "Series" property that is exposed. FYI, I'm talking about the "Chart" class present in the System.web.Helpers assembly that can be used in asp.net mvc app and its not same as the "chart" that comes in the System.data.visualization assembly that comes with asp.net web forms app


to show a legend...

chart.Legends.Add(new Legend("Default") { Docking = Docking.Right });


You can specify range of colors that you want in .SeriesColors Property of Chart,

.SeriesColors(new string[] { "#952F91", "#00A9AA", "#EA7EB8"  /*...range of colors */ })
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜