原型更新数据

发布于 2024-10-29 08:03:55 字数 2737 浏览 4 评论 0原文

我尝试了很多使用 jquery 更新 protovis 但没有任何反应。这是我的代码。

<script type="text/javascript+protovis">
var minnesota =  [{name:"job 1", values:[182904,196530,203944,192492,77393,81243]}]

$(document).ready(function(){
draw();
$("button").click(function(){
minnesota = [{name:"changed job", values:[342,34234,2342,543]}]
draw();
});

});



    function draw(){
    var w = 200,
        h = 30,
        numberFormat = pv.Format.number(),
        dateFormat = pv.Format.date("%B %Y");

    /* Color by maximum number of people employed in that job. */
    var c = pv.Scale.log(minnesota, function(d) pv.max(d.values))
        .range("#ccc", "#1f77b4");

    /* Tile the visualization for each job. */
    var vis = new pv.Panel()
        .data(minnesota)
        .width(w)
        .height(h + 10)
        .top(6)
        .left(6)
        .right(6)
        .bottom(6);

    /* A panel instance to store scales (x, y) and the mouseover index (i). */
    var panel = vis.add(pv.Panel)
        .def("i", -1)
        .def("x", function(d) pv.Scale.linear(d.values, pv.index).range(0, w))
        .def("y", function(d) pv.Scale.linear(0, pv.max(d.values)).range(0, h))
        .bottom(10)
        .events("all")
        .event("mousemove", pv.Behavior.point(Infinity).collapse("y"));

    /* The area. */
    panel.add(pv.Area)
        .data(function(d) d.values)
        .fillStyle(function(d, p) panel.i() < 0 ? c(pv.max(p.values)) : "#2ca02c")
        .left(function() panel.x()(this.index))
        .height(function(d) panel.y()(d))
        .bottom(0)
        .event("point", function() panel.i(this.index))
        .event("unpoint", function() panel.i(-1));

    /* The x-axis. */
    panel.add(pv.Rule)
        .bottom(0);

    /* The mouseover dot. */
    panel.add(pv.Dot)
        .visible(function() panel.i() >= 0)
        .left(function() panel.x()(panel.i()))
        .bottom(function(d) panel.y()(d.values[panel.i()]))
        .fillStyle("#ff7f0e")
        .strokeStyle(null)
        .size(10);

    /* The label: either the job name, or the month and value. */
    panel.add(pv.Label)
        .bottom(-1)
        .textBaseline("top")
        .left(function() panel.i() < 0 ? 0 : null)
        .right(function() panel.i() < 0 ? null : 0)
        .textAlign(function() panel.i() < 0 ? "left" : "right")
        .textStyle(function() panel.i() < 0 ? "#999" : "#000")
        .text(function(d) panel.i() < 0 ? d.name
            : dateFormat(new Date(2000, panel.i() * 3, 1))
            + ": " + numberFormat(d.values[panel.i()]));

    vis.render();
    }
        </script>

当文档准备好时,它会创建图表,但是当我单击按钮时,它不会重新绘制图表。我已经用 firebug 检查了新的数据值,这很好,但它不会更新图表值。 我已经尝试了很多方法来寻找解决方案,但未能成功。有什么窍门吗?

I have tried a lot to update protovis using jquery but nothing happens. here is my code.

<script type="text/javascript+protovis">
var minnesota =  [{name:"job 1", values:[182904,196530,203944,192492,77393,81243]}]

$(document).ready(function(){
draw();
$("button").click(function(){
minnesota = [{name:"changed job", values:[342,34234,2342,543]}]
draw();
});

});



    function draw(){
    var w = 200,
        h = 30,
        numberFormat = pv.Format.number(),
        dateFormat = pv.Format.date("%B %Y");

    /* Color by maximum number of people employed in that job. */
    var c = pv.Scale.log(minnesota, function(d) pv.max(d.values))
        .range("#ccc", "#1f77b4");

    /* Tile the visualization for each job. */
    var vis = new pv.Panel()
        .data(minnesota)
        .width(w)
        .height(h + 10)
        .top(6)
        .left(6)
        .right(6)
        .bottom(6);

    /* A panel instance to store scales (x, y) and the mouseover index (i). */
    var panel = vis.add(pv.Panel)
        .def("i", -1)
        .def("x", function(d) pv.Scale.linear(d.values, pv.index).range(0, w))
        .def("y", function(d) pv.Scale.linear(0, pv.max(d.values)).range(0, h))
        .bottom(10)
        .events("all")
        .event("mousemove", pv.Behavior.point(Infinity).collapse("y"));

    /* The area. */
    panel.add(pv.Area)
        .data(function(d) d.values)
        .fillStyle(function(d, p) panel.i() < 0 ? c(pv.max(p.values)) : "#2ca02c")
        .left(function() panel.x()(this.index))
        .height(function(d) panel.y()(d))
        .bottom(0)
        .event("point", function() panel.i(this.index))
        .event("unpoint", function() panel.i(-1));

    /* The x-axis. */
    panel.add(pv.Rule)
        .bottom(0);

    /* The mouseover dot. */
    panel.add(pv.Dot)
        .visible(function() panel.i() >= 0)
        .left(function() panel.x()(panel.i()))
        .bottom(function(d) panel.y()(d.values[panel.i()]))
        .fillStyle("#ff7f0e")
        .strokeStyle(null)
        .size(10);

    /* The label: either the job name, or the month and value. */
    panel.add(pv.Label)
        .bottom(-1)
        .textBaseline("top")
        .left(function() panel.i() < 0 ? 0 : null)
        .right(function() panel.i() < 0 ? null : 0)
        .textAlign(function() panel.i() < 0 ? "left" : "right")
        .textStyle(function() panel.i() < 0 ? "#999" : "#000")
        .text(function(d) panel.i() < 0 ? d.name
            : dateFormat(new Date(2000, panel.i() * 3, 1))
            + ": " + numberFormat(d.values[panel.i()]));

    vis.render();
    }
        </script>

when document ready then it create charts but when I click button then it doesn't re draw chart. I've checked new data values with firebug which is fine but it doesn't update charts values.
I've tried a lot to find solution but couldn't succeeded. Any trick?

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

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

发布评论

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

评论(2

山色无中 2024-11-05 08:03:55

对我来说,您的解决方案绘制了一个额外的图表,而不是使用新数据重新绘制相同的图表。

我在您的代码中移动了一些内容并让更新正常工作。您只需更新数据并调用 vis.redraw,而不是在每次单击时设置整个可视化。

一探究竟:
https://gist.github.com/906247

For me, your solution drew an additional chart, instead of redrawing the same chart with new data.

I moved a few things in your code around and have the update working. You simply need to update the data and call vis.redraw, not set up your entire visualization on each click.

Check it out:
https://gist.github.com/906247

不及他 2024-11-05 08:03:55

我只是在每个更改数据的函数末尾使用 vis.render();

I just use vis.render(); at the end of each function that changed the data.

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