拉斐尔饼图,放动态数据?

发布于 2024-11-01 05:06:57 字数 889 浏览 0 评论 0原文

我想将我的动态数据放入拉斐尔饼图中。我无法在 javascript 代码的 window.onload() 之外编写以下代码。有人在rapahael饼图中添加动态数据吗?这是我当前的代码:

<script language="javascript" type="text/javascript">
    window.onload = function () {
        var r = Raphael("holder");
        r.g.txtattr.font = "12px 'Fontin Sans', Fontin-Sans, sans-serif";
        r.g.text(320, 100, "Interactive Pie Chart").attr({"font-size": 20});
        var pie = r.g.piechart(320, 240, 100, [55, 20, 13, 32, 5, 1, 2], {legend: ["%%.%%" , "%%.%%","%%.%%"], legendpos: "west"});
    }
</script>

Insted of this 55, 20, 13, 32, 5, 1, 2 data 我想将从数据库返回的数据写入我的 JSP 页面中。我怎样才能添加它? 的数据

<c:forEach var="Detail" items="${DetailRow.List.}">'${Detail.Text}',${Detail.Count}</c:forE­ach>

我有想要将 ${Detail.Text}',${Detail.Count} 添加到饼图中 。如何将其转换为 ([[],[]..])

I want to put my dynamic data into the raphael pie chart. I can't write the following code outside window.onload() of my javascript code. Did anyone add dynamic data in the rapahael pie chart? This is my current code:

<script language="javascript" type="text/javascript">
    window.onload = function () {
        var r = Raphael("holder");
        r.g.txtattr.font = "12px 'Fontin Sans', Fontin-Sans, sans-serif";
        r.g.text(320, 100, "Interactive Pie Chart").attr({"font-size": 20});
        var pie = r.g.piechart(320, 240, 100, [55, 20, 13, 32, 5, 1, 2], {legend: ["%%.%%" , "%%.%%","%%.%%"], legendpos: "west"});
    }
</script>

Insted of this 55, 20, 13, 32, 5, 1, 2 data I want to write my data returned from database in my JSP page. How can I add that? I'm having the data

<c:forEach var="Detail" items="${DetailRow.List.}">'${Detail.Text}',${Detail.Count}</c:forE­ach>

I want to add ${Detail.Text}',${Detail.Count} into the pie chart. How can I convert it into ([[],[]..])?

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

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

发布评论

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

评论(2

偏闹i 2024-11-08 05:06:57

我就是这样做的。我正在使用 ASP.Net MVC,但您可以使用任何框架或语言。

values、legendvalues 和sectorLinkValues 是在页面中创建的.Net List 对象。

%>
<script type="text/javascript">
    $(document).ready(function () {
        <% =truViewPOC.Helpers.GraphaelChartHelper.GetJavaScript(100, 150, 90, title, values, legendValues, sectorLinkValues, null) %>
     });

这是我的 GetJavaScript 方法:

StringBuilder javaScriptBuilder = new StringBuilder();
javaScriptBuilder.Append("window.onload = function () {var r = Raphael('holder');");
AppendCenterCoordinates(ref javaScriptBuilder);

AppendLegendInfo(ref javaScriptBuilder, title);
AppendPieChartInfo(ref javaScriptBuilder, xCoord, YCoord,radius, values, legendValues,sectorLinkValues);
AppendFunctionInfo(ref javaScriptBuilder);
javaScriptBuilder.Append("}");

private static void AppendFunctionInfo(ref StringBuilder javaScriptBuilder)
        {
            javaScriptBuilder.Append("pie.hover(function () {this.sector.stop();this.sector.scale(1.1, 1.1, this.cx, this.cy);if (this.label) {this.label[0].stop();this.label[0].scale(1.5);this.label[1].attr({ 'font-weight': 800 });}}, function () {this.sector.animate({ scale: [1, 1, this.cx, this.cy] }, 500, 'bounce');if (this.label) {this.label[0].animate({ scale: 1 }, 500, 'bounce');this.label[1].attr({ 'font-weight': 400 });}}); ");
            javaScriptBuilder.Append("pie.each(function(){ this.sector.label=this.sector.value;   })");
        }
        private static void AppendPieChartInfo(ref StringBuilder javaScriptBuilder, int xCoord, int YCoord, int radius, List<decimal> values, List<string> legendValues, List<string> sectorLinkValues)
        {
            javaScriptBuilder.AppendFormat("var pie = r.g.piechart({0},{1},{2},{3},", xCoord, YCoord, radius, ConvertValuesToString(values));
            javaScriptBuilder.Append("{");
            javaScriptBuilder.AppendFormat("legend: {0}, legendpos: 'east', href:{1}",ConvertValuesToString(legendValues),ConvertValuesToString(sectorLinkValues));
            javaScriptBuilder.Append("});");
        }

private static void AppendLegendInfo(ref StringBuilder javaScriptBuilder, string title)
        {
            javaScriptBuilder.Append("r.g.text(140, 24, " + "'" + title +  "'" + ").attr({ 'font-size': 16 });");
        }

 private static string ConvertValuesToString(List<decimal> values)
        {
            string finalString = "[";


            for (int i = 0; i < values.Count; i++)
            {
                finalString += values[i].ToString();
                if (i<values.Count-1)
                {
                     finalString+= ",";
                }


            }
            finalString += "]";
            return finalString;

希望这会有所帮助。如果您对此代码有任何疑问,请告诉我。谢谢。维卡斯
}

This is how I do it. I am using ASP.Net MVC, But you can use any framework or language.

values, legendvalues and sectorLinkValues are .Net List object created in the page.

%>
<script type="text/javascript">
    $(document).ready(function () {
        <% =truViewPOC.Helpers.GraphaelChartHelper.GetJavaScript(100, 150, 90, title, values, legendValues, sectorLinkValues, null) %>
     });

And here is my GetJavaScript method:

StringBuilder javaScriptBuilder = new StringBuilder();
javaScriptBuilder.Append("window.onload = function () {var r = Raphael('holder');");
AppendCenterCoordinates(ref javaScriptBuilder);

AppendLegendInfo(ref javaScriptBuilder, title);
AppendPieChartInfo(ref javaScriptBuilder, xCoord, YCoord,radius, values, legendValues,sectorLinkValues);
AppendFunctionInfo(ref javaScriptBuilder);
javaScriptBuilder.Append("}");

private static void AppendFunctionInfo(ref StringBuilder javaScriptBuilder)
        {
            javaScriptBuilder.Append("pie.hover(function () {this.sector.stop();this.sector.scale(1.1, 1.1, this.cx, this.cy);if (this.label) {this.label[0].stop();this.label[0].scale(1.5);this.label[1].attr({ 'font-weight': 800 });}}, function () {this.sector.animate({ scale: [1, 1, this.cx, this.cy] }, 500, 'bounce');if (this.label) {this.label[0].animate({ scale: 1 }, 500, 'bounce');this.label[1].attr({ 'font-weight': 400 });}}); ");
            javaScriptBuilder.Append("pie.each(function(){ this.sector.label=this.sector.value;   })");
        }
        private static void AppendPieChartInfo(ref StringBuilder javaScriptBuilder, int xCoord, int YCoord, int radius, List<decimal> values, List<string> legendValues, List<string> sectorLinkValues)
        {
            javaScriptBuilder.AppendFormat("var pie = r.g.piechart({0},{1},{2},{3},", xCoord, YCoord, radius, ConvertValuesToString(values));
            javaScriptBuilder.Append("{");
            javaScriptBuilder.AppendFormat("legend: {0}, legendpos: 'east', href:{1}",ConvertValuesToString(legendValues),ConvertValuesToString(sectorLinkValues));
            javaScriptBuilder.Append("});");
        }

private static void AppendLegendInfo(ref StringBuilder javaScriptBuilder, string title)
        {
            javaScriptBuilder.Append("r.g.text(140, 24, " + "'" + title +  "'" + ").attr({ 'font-size': 16 });");
        }

 private static string ConvertValuesToString(List<decimal> values)
        {
            string finalString = "[";


            for (int i = 0; i < values.Count; i++)
            {
                finalString += values[i].ToString();
                if (i<values.Count-1)
                {
                     finalString+= ",";
                }


            }
            finalString += "]";
            return finalString;

Hope this helps..Let me know if you have any questions about this code. Thanks. Vikas
}

偏爱自由 2024-11-08 05:06:57

Kiran,

如果我很好地理解你,你想做这样的事情:

window.onload = function () {
  var r = Raphael("holder"),
  mydata = //AJAX call to get data from Database
  //Parse mydata to have the format you need ([[],[]..])...
  r.g.piechart(320, 240, 100,mydata);
}; 

如果你可以在你的问题中显示一些代码,那就太好了:)。但我相信的想法是,对可以从数据库获取数据的脚本进行 AJAX 调用。

Kiran,

If I understood you well, you want to do something like this:

window.onload = function () {
  var r = Raphael("holder"),
  mydata = //AJAX call to get data from Database
  //Parse mydata to have the format you need ([[],[]..])...
  r.g.piechart(320, 240, 100,mydata);
}; 

If you could show some code in your question that would be nice :). But the idea I believe is this, do an AJAX call to a script that can get data from the database.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文