DotNet HighCharts ,用查询结果填充饼图

发布于 2025-01-04 11:26:54 字数 2748 浏览 1 评论 0原文

我需要用查询(LINQ To SQL)结果的数据填充饼图,

问题是我无法在这段代码中添加 foreach 来插入我的数据,而不是静态的 Firefox、Chrome、IE 等...

        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();







        }

实际上,我需要能够插入这样的东西:

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

但是VS不允许我做这样的事情 预先感谢您的帮助...

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 ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

山田美奈子 2025-01-11 11:26:54

您可以为 DAaccess.LibDataVersion.LibDataVersion.GetNumberOfCompaniesUsingEachVersions() 返回的任何类型创建一个扩展方法,并让它以饼形系列格式返回结果。

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

然后,在包含静态代码的代码中,只需将其替换为:

Data = new Data(test.ToPieChartSeries())

或者,您可以让 ToPieChartSeries 方法返回正在寻找的 Data 对象通过 Series 对象。

虽然我没有使用过这个 DotNet.HighCharts 项目,但我已经为几个 MVC 项目使用并构建了自己的 HighCharts 对象。从表面上看,这似乎最终做了与我相同的事情:创建一个可以序列化为 JSON 并由 HighCharts javascript 库识别的 .NET 对象。

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.

天荒地未老 2025-01-11 11:26:54
SetSeries(new Series {
                Type = ChartTypes.Pie,
                Data = new Data(test.Select(d => new { Name = d.name, Y = d.count }).ToArray())
            })
SetSeries(new Series {
                Type = ChartTypes.Pie,
                Data = new Data(test.Select(d => new { Name = d.name, Y = d.count }).ToArray())
            })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文