Anish
Anish

Reputation: 3172

Values in a pie chart

How do you display the values of each pie in a pie chart using ChartHelper? I am using MVC3/Razor syntax.

Trying to do something like this:

Pie

The image is from this tutorial for ChartHelper in MVC:

My code:

var bytes = new Chart(600, 300).AddSeries(
                    chartType: "pie",
                    legend: "Sales in Store per Payment Collected",
                    xValue: viewModel.SalesInStorePerPaymentCollected.XValues,
                    yValues: viewModel.SalesInStorePerPaymentCollected.YValues
                    )
                    .GetBytes("png");


            return File(bytes, "image/png");

Upvotes: 3

Views: 46217

Answers (3)

DaveShaw
DaveShaw

Reputation: 52808

I did it by using the System.Web.UI.DataVisualization.Charting.Chart class.

Here is the code in my Controller:

public ActionResult Chart()
{
    Chart chart = new Chart();
    chart.ChartAreas.Add(new ChartArea());

    chart.Series.Add(new Series("Data"));
    chart.Series["Data"].ChartType = SeriesChartType.Pie;
    chart.Series["Data"]["PieLabelStyle"] = "Outside"; 
    chart.Series["Data"]["PieLineColor"] = "Black";
    chart.Series["Data"].Points.DataBindXY(
        data.Select(data => data.Name.ToString()).ToArray(), 
        data.Select(data => data.Count).ToArray());
    //Other chart formatting and data source omitted.

    MemoryStream ms = new MemoryStream();
    chart.SaveImage(ms, ChartImageFormat.Png);
    return File(ms.ToArray(), "image/png");
}

And the View:

<img alt="alternateText" src="@Url.Action("Chart")" />

Upvotes: 13

Yakir Manor
Yakir Manor

Reputation: 4839

my answer and solution (works with explanation):

this suits the MVC 4 and MVC 3 with .NET 4 framework and adding reference to System.Web.DataVisualization.dll and not the .net System.Web.Helpers, the DataVisualization.dll can be found at http://www.codeproject.com/Articles/125230/ASP-NET-MVC-Chart-Control

ChartApplication->External References

more info on charts with the DataVisualization can be found there.

never mind, it could be replaced with:

        public ActionResult Chart()
    {
        Chart chart = new Chart();

        chart.ChartAreas.Add(new ChartArea());

        chart.Series.Add(new Series("Data"));
        chart.Legends.Add(new Legend("Stores"));
        chart.Series["Data"].ChartType = SeriesChartType.Spline;
        chart.Series["Data"].Points.AddXY(1.0, 5.0);
        chart.Series["Data"].Points.AddXY(2.0, 9.0);
        /*
        chart.Series["Data"]["PieLabelStyle"] = "Outside";
        chart.Series["Data"]["PieLineColor"] = "Black";
        */
        /*
            int ptIdx = chart.Series["Data"].Points.AddXY(1.0, 5.0);
            DataPoint pt = chart.Series["Data"].Points[ptIdx];
            pt.LegendText = "#VALX: #VALY";
            pt.LegendUrl = "/Contact/Details/Hey";
        */
        /*
        chart.Series["Data"].Label = "#PERCENT{P0}";
        chart.Series["Data"].Font = new Font("Segoe UI", 8.0f, FontStyle.Bold);
        chart.Series["Data"].ChartType = SeriesChartType.Pie;
        chart.Series["Data"]["PieLabelStyle"] = "Outside";
        chart.Series["Data"].Legend = "Stores";
        chart.Legends["Stores"].Docking = Docking.Bottom;
        */
        var returnStream = new MemoryStream();
        chart.ImageType = ChartImageType.Png;
        chart.SaveImage(returnStream);
        returnStream.Position = 0;
        return new FileStreamResult(returnStream, "image/png");
    }

or (for the mvc 4 and mvc 3 with .net 4 and System.Web.Helpers):

        public ActionResult Chart()
    {
        var bytes = new Chart(600, 300).AddSeries(
                            chartType: "pie",
                            legend: "Sales in Store per Payment Collected",
                            xValue: new[] { "Jan", "Feb", "Mar", "Apr", "May" },
                            yValues: new[] { "20", "20", "40", "10", "10" }
                            )
                            .Write("png");
        return null;
    }

and of course you need in both to add to the .cshtml the following:

<img src="/Home/Chart" alt="" /> 

Upvotes: 4

Anish
Anish

Reputation: 3172

My solution thanks to DaveShaw. Needs little bit more tweaking, but gives me most of what I need.

        Chart chart = new Chart();

        chart.ChartAreas.Add(new ChartArea());

        chart.Series.Add(new Series("Data"));
        chart.Legends.Add(new Legend("Stores"));
        chart.Series["Data"].ChartType = SeriesChartType.Pie;
        chart.Series["Data"]["PieLabelStyle"] = "Outside";
        chart.Series["Data"]["PieLineColor"] = "Black";
        for (int x = 0; x < viewModel.SalesInStorePerPaymentCollected.XValues.Length; x++)
        {
           int ptIdx = chart.Series["Data"].Points.AddXY(
                viewModel.SalesInStorePerPaymentCollected.XValues[x],
                viewModel.SalesInStorePerPaymentCollected.YValues[x]);
           DataPoint pt = chart.Series["Data"].Points[ptIdx];
           pt.LegendText = "#VALX: #VALY";
           pt.LegendUrl = "/Contact/Details/Hey";
        }

        chart.Series["Data"].Label = "#PERCENT{P0}";
        chart.Series["Data"].Font = new Font("Segoe UI", 8.0f, FontStyle.Bold);
        chart.Series["Data"].ChartType = SeriesChartType.Pie;
        chart.Series["Data"]["PieLabelStyle"] = "Outside";
        chart.Series["Data"].Legend = "Stores";
        chart.Legends["Stores"].Docking = Docking.Bottom;

        var returnStream = new MemoryStream();
        chart.ImageType = ChartImageType.Png;
        chart.SaveImage(returnStream);
        returnStream.Position = 0;
        return new FileStreamResult(returnStream, "image/png");

Renders to this:

Labels blackened for privacy

Upvotes: 5

Related Questions