user1173169
user1173169

Reputation:

DotNet HighCharts , Populates a pie with the result of a query

I need to populate a pie with data which are the result of a query (LINQ To SQL)

The problem is i cannot manage to add a foreach inside this code to insert my data instead of the static Firefox,Chrome,IE ect ...

        protected void Page_Load(object sender, EventArgs e)
        {
            //RepeaterVersionsForPie.DataSource = DAaccess.LibDataVersion.LibDataVersion.GetNumberOfCompaniesUsingEachVersions();
            //RepeaterVersionsForPie.DataBind();

            var test = DAaccess.LibDataVersion.LibDataVersion.GetNumberOfCompaniesUsingEachVersions();

            Highcharts chart = new Highcharts("chart")
.InitChart(new Chart { PlotShadow = false })
.SetTitle(new Title { Text = "Browser market shares at a specific website, 2010" })
.SetTooltip(new Tooltip { Formatter = "function() { return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %'; }" })
.SetPlotOptions(new PlotOptions
{
    Pie = new PlotOptionsPie
    {
        AllowPointSelect = true,
        Cursor = Cursors.Pointer,
        DataLabels = new PlotOptionsPieDataLabels
        {
            Color = ColorTranslator.FromHtml("#000000"),
            ConnectorColor = ColorTranslator.FromHtml("#000000"),
            Formatter = "function() { return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %'; }"
        },
        Point = new PlotOptionsPiePoint
        {
            Events = new PlotOptionsPiePointEvents
            {
                Click = "function() { alert (this.category +': '+ this.y); }"

            }
        }

    }
})
.SetSeries(new Series
{
    Type = ChartTypes.Pie,
    Name = "Browser share",
    Data = new Data(new object[]
                                           {
                                               new object[] { "Firefox", 45.0 },
                                               new object[] { "IE", 26.8 },
                                               new DotNet.Highcharts.Options.Point
                                               {
                                                   Name = "Chrome",
                                                   Y = 12.8,
                                                   Sliced = true,
                                                   Selected = true
                                               },
                                               new object[] { "Safari", 8.5 },
                                               new object[] { "Opera", 6.2 },
                                               new object[] { "Others", 0.7 }
                                           })
});
            ltrChart.Text = chart.ToHtmlString();







        }

Actually , i need to be able to insert something like this:

foreach ( var item in test )
{
new object[] { item.name, item.count}
}

But VS doesn't allow me to do such thing Thanks in advance for your help ...

Upvotes: 3

Views: 6262

Answers (2)

Mayank
Mayank

Reputation: 958

SetSeries(new Series {
                Type = ChartTypes.Pie,
                Data = new Data(test.Select(d => new { Name = d.name, Y = d.count }).ToArray())
            })

Upvotes: 2

Jonathan Bates
Jonathan Bates

Reputation: 1835

You could create an extension method for whatever type DAaccess.LibDataVersion.LibDataVersion.GetNumberOfCompaniesUsingEachVersions() returns and have it return the results in the Pie-shaped Series format.

public static class DotNetHighChartsExtensions
{
    public static object[] ToPieChartSeries(this WhateverThatTypeIs data)
    {
        var returnObject = new List<object>();

        foreach ( var item in data )
        {
            returnObject.Add(new object[] { item.name, item.count});
        }

        return returnObject.ToArray();
    }
}

Then in your code where you have your static code, you'd just replace it with:

Data = new Data(test.ToPieChartSeries())

Alternatively, you could have the ToPieChartSeries method return the Data object that is being sought for by the Series object.

While I haven't worked with this DotNet.HighCharts project, I have worked with and built my own HighCharts objects for a couple of MVC projects. On the face of it, it looks like this are ultimately doing the same thing I did: Create a .NET object that could be serialized as JSON and recognized by the HighCharts javascript library.

Upvotes: 3

Related Questions