3

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");
DaveShaw
  • 52,123
  • 16
  • 112
  • 141
Anish
  • 3,045
  • 3
  • 27
  • 29
  • You can also achieve that image by combining both client side and server side. Drag and drop a Chart control, play with html and css to position it. Set the Series CustomProperties="PieLineColor=Black, PieLabelStyle=Outside", customize Points to your desire and then handle the outside label values from your server side code by calling 'Chart.Series[0].Points[0].SetValueY(value)' to set the value of each pie piece and 'Chart1.Series[0].Points[0].Label = value' to set the outside label text. Not sure how you separate a pie piece. Maybe playing around with the xAxis – Jcorretjer Feb 22 '17 at 03:37

3 Answers3

13

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")" />
Suraj Shrestha
  • 1,790
  • 1
  • 25
  • 51
DaveShaw
  • 52,123
  • 16
  • 112
  • 141
  • My chart is from the System.Web.Helpers namespace. It looks like your chart is from System.Web.UI.DataVisualization.Charting. How do you return System.Web.UI.DataVisualization.Charting.Chart to an MVC View? – Anish Dec 06 '11 at 17:57
  • 1
    @Anish You're quite right, sorry. I've updated my sample to include the full Controller Method and View. – DaveShaw Dec 06 '11 at 20:20
5

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

Anish
  • 3,045
  • 3
  • 27
  • 29
4

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="" /> 
Yakir Manor
  • 4,687
  • 1
  • 32
  • 25