var chart; "/>
首页 \ 问答 \ 将JSON数据与jQuery highcharts插件一起使用[关闭](Using JSON data with the jQuery highcharts plugin [closed])

将JSON数据与jQuery highcharts插件一起使用[关闭](Using JSON data with the jQuery highcharts plugin [closed])

<script type="text/javascript">
    var chart;
    $(document).ready(function() {
        // Define the options
        var options = {
            chart: {
                renderTo: 'container'
            },

            title: {
                text: 'Daily visits at www.highcharts.com'
            },

            subtitle: {
                text: 'Source: Google Analytics'
            },

            xAxis: {
                type: 'datetime',
                tickInterval: 7 * 24 * 3600 * 1000, // One week
                tickWidth: 0,
                gridLineWidth: 1,
                labels: {
                    align: 'left',
                    x: 3,
                    y: -3 
                }
            },

            yAxis: [{ // Left Y axis
                title: {
                    text: null
                },
                labels: {
                    align: 'left',
                    x: 3,
                    y: 16,
                    formatter: function() {
                        return Highcharts.numberFormat(this.value, 0);
                    }
                },
                showFirstLabel: false
            }, { // right y axis
                linkedTo: 0,
                gridLineWidth: 0,
                opposite: true,
                title: {
                    text: null
                },
                labels: {
                    align: 'right',
                    x: -3,
                    y: 16,
                    formatter: function() {
                        return Highcharts.numberFormat(this.value, 0);
                    }
                },
                showFirstLabel: false
            }],

            legend: {
                align: 'left',
                verticalAlign: 'top',
                y: 20,
                floating: true,
                borderWidth: 0
            },

            tooltip: {
                shared: true,
                crosshairs: true
            },

            plotOptions: {
                series: {
                    cursor: 'pointer',
                    point: {
                        events: {
                            click: function() {
                                hs.htmlExpand(null, {
                                    pageOrigin: {
                                        x: this.pageX, 
                                        y: this.pageY
                                    },
                                    headingText: this.series.name,
                                    maincontentText: Highcharts.dateFormat('%A, %b %e, %Y', this.x) +':<br/> '+ 
                                        this.y +' visits',
                                    width: 200
                                });
                            }
                        }
                    },
                    marker: {
                        lineWidth: 1
                    }
                }
            },

            series: [{
                name: 'All visits',
                lineWidth: 4,
                marker: {
                    radius: 4
                }
            }, {
                name: 'New visitors'
            }]
        }

        // Load data asynchronously using jQuery. On success, add the data
        // to the options and initiate the chart.
        // This data is obtained by exporting a GA custom report to TSV.
        // http://api.jquery.com/jQuery.get/
        jQuery.get('analytics.tsv', null, function(tsv) {
            var lines = [],
                listen = false,
                date,

                // Set up the two data series.
                allVisits = [],
                newVisitors = [];

            try {
            // Split the data return into lines and parse them.
            tsv = tsv.split(/\n/g);
            jQuery.each(tsv, function(i, line) {
                // Listen for data lines between the Graph and Table headers.
                if (tsv[i - 3] == '# Graph') {
                    listen = true;
                } else if (line == '' || line.charAt(0) == '#') {
                    listen = false;
                }

                // All data lines start with a double quote.
                if (listen) {
                    line = line.split(/\t/);
                    date = Date.parse(line[0] +' UTC');

                    allVisits.push([
                        date, 
                        parseInt(line[1].replace(',', ''), 10)
                    ]);
                    newVisitors.push([
                        date, 
                        parseInt(line[2].replace(',', ''), 10)
                    ]);
                }
            });
            } catch (e) { alert(e.message) }
            options.series[0].data = allVisits;
            options.series[1].data = newVisitors;

            chart = new Highcharts.Chart(options);
        });
    });
</script>

上面是jQuery插件的示例代码'highcharts'。 我试图从JSON文件中获取数据,如果JSON字符串为: { name: 'allVisits', data: [1, 0, 4] }, { name: 'newVisits', data: [5, 7, 3] }

示例文件是从'tsv'文件获取数据,因此我试图从JSON文件中获取数据。


<script type="text/javascript">
    var chart;
    $(document).ready(function() {
        // Define the options
        var options = {
            chart: {
                renderTo: 'container'
            },

            title: {
                text: 'Daily visits at www.highcharts.com'
            },

            subtitle: {
                text: 'Source: Google Analytics'
            },

            xAxis: {
                type: 'datetime',
                tickInterval: 7 * 24 * 3600 * 1000, // One week
                tickWidth: 0,
                gridLineWidth: 1,
                labels: {
                    align: 'left',
                    x: 3,
                    y: -3 
                }
            },

            yAxis: [{ // Left Y axis
                title: {
                    text: null
                },
                labels: {
                    align: 'left',
                    x: 3,
                    y: 16,
                    formatter: function() {
                        return Highcharts.numberFormat(this.value, 0);
                    }
                },
                showFirstLabel: false
            }, { // right y axis
                linkedTo: 0,
                gridLineWidth: 0,
                opposite: true,
                title: {
                    text: null
                },
                labels: {
                    align: 'right',
                    x: -3,
                    y: 16,
                    formatter: function() {
                        return Highcharts.numberFormat(this.value, 0);
                    }
                },
                showFirstLabel: false
            }],

            legend: {
                align: 'left',
                verticalAlign: 'top',
                y: 20,
                floating: true,
                borderWidth: 0
            },

            tooltip: {
                shared: true,
                crosshairs: true
            },

            plotOptions: {
                series: {
                    cursor: 'pointer',
                    point: {
                        events: {
                            click: function() {
                                hs.htmlExpand(null, {
                                    pageOrigin: {
                                        x: this.pageX, 
                                        y: this.pageY
                                    },
                                    headingText: this.series.name,
                                    maincontentText: Highcharts.dateFormat('%A, %b %e, %Y', this.x) +':<br/> '+ 
                                        this.y +' visits',
                                    width: 200
                                });
                            }
                        }
                    },
                    marker: {
                        lineWidth: 1
                    }
                }
            },

            series: [{
                name: 'All visits',
                lineWidth: 4,
                marker: {
                    radius: 4
                }
            }, {
                name: 'New visitors'
            }]
        }

        // Load data asynchronously using jQuery. On success, add the data
        // to the options and initiate the chart.
        // This data is obtained by exporting a GA custom report to TSV.
        // http://api.jquery.com/jQuery.get/
        jQuery.get('analytics.tsv', null, function(tsv) {
            var lines = [],
                listen = false,
                date,

                // Set up the two data series.
                allVisits = [],
                newVisitors = [];

            try {
            // Split the data return into lines and parse them.
            tsv = tsv.split(/\n/g);
            jQuery.each(tsv, function(i, line) {
                // Listen for data lines between the Graph and Table headers.
                if (tsv[i - 3] == '# Graph') {
                    listen = true;
                } else if (line == '' || line.charAt(0) == '#') {
                    listen = false;
                }

                // All data lines start with a double quote.
                if (listen) {
                    line = line.split(/\t/);
                    date = Date.parse(line[0] +' UTC');

                    allVisits.push([
                        date, 
                        parseInt(line[1].replace(',', ''), 10)
                    ]);
                    newVisitors.push([
                        date, 
                        parseInt(line[2].replace(',', ''), 10)
                    ]);
                }
            });
            } catch (e) { alert(e.message) }
            options.series[0].data = allVisits;
            options.series[1].data = newVisitors;

            chart = new Highcharts.Chart(options);
        });
    });
</script>

Above is example code for a jQuery plugin, 'highcharts'. I am trying to get the data from a JSON file if the JSON string is as: { name: 'allVisits', data: [1, 0, 4] }, { name: 'newVisits', data: [5, 7, 3] }.

The example file is getting the data from a 'tsv' file, so I am trying to get the data from the JSON file instead.


原文:https://stackoverflow.com/questions/6423687
更新时间:2023-04-13 08:04

最满意答案

你的需要是绝对可以理解的。 但是我们应该很快就会想到Spring会做什么: - 当应用服务器启动时,执行startuphook,它会搜索整个类路径,查找用Jax-rs Annotations分配的每个类,并将它们初始化或只是在“路由器”上注册它们。

所以,如果你想要那样,你就可以拥有它,但你必须通过自己做到这一点。 对不起:D。

例如:

class Server extends AbstractVerticle {

    @Override
    public void start() throws Exception {
        List<AbstractVerticle> verticles = searchForVerticlesWithMyAnnotation();
        verticles.forEach((V) = > router.add(V));
    }

}

@MyOwnJax(path = "/blaa")
public class TestService {
}

@interface MyOwnJax {
    String path();
}

方法“searchForVerticlesWIthMyAnnotation”在这里是棘手的事情。 它不应该慢。 但是如果你使用Spring,你可以使用类似的东西: org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider

或者在这里看到: Stackoverflow:搜索注释@runtime

但这是一个很大的但是在这里。 ;)也许你有一个更好的主意,然后Spring来制作你的REST api? 在我看来,Spring真的是“klobby”,而Vertx.x非常流畅。 (对不起,我的意见不是真实的。)

我在我的应用程序中使用DI方法。 意思是:

router.route(HttpMethod.GET,
 "/user/login").handler(injector.getInstance(ILoginUser.class));

使用普通的guice框架作为注入器。 虽然这只是一个接口,但您必须在启动服务器的Verticle中更改某些内容之前进行大的更改。 (实际上大部分只是你必须添加或删除路径)

概要:

  • 如果你想要一个Spring方法,你必须使用反射或使用反射的库。 缺点:启动性能,有时甚至有点神奇,很难找到错误/调试。 好处:易于测试,非常容易扩展功能

  • 在自己的路径上注册Verticle。 缺点:您必须在“服务器”上添加/删除路径。 好处:启动 - 性能,没有魔力,完全控制发生的事情和时间。

这只是一个简短的总结,并没有提到很多要点。 但我希望这能回答你的问题。 如果你有一些问题,那就写吧!

Jeerze,

西米


Your need is absolutly understandable. But we should maybe shortly think about what spring does: - when Application server starts up a startuphook is executed which searches the whole classpath for every class anotated with Jax-rs Annotations and initalizeses them or just registers them on a "router".

So if you want that, you can have that but you have to do that by ur self. Im sorry :D.

e.g:

class Server extends AbstractVerticle {

    @Override
    public void start() throws Exception {
        List<AbstractVerticle> verticles = searchForVerticlesWithMyAnnotation();
        verticles.forEach((V) = > router.add(V));
    }

}

@MyOwnJax(path = "/blaa")
public class TestService {
}

@interface MyOwnJax {
    String path();
}

The method "searchForVerticlesWIthMyAnnotation" is the tricky thing here. It should not be to slow. But if you use Spring anyway you can use something like: org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider

or see here : Stackoverflow: Search for Annotations @runtime

BUT and this is a big but here. ;) Maybe you have a better idea then Spring to make your REST api ? Spring is really "klobby" in my opinion whereas Vertx.x is really smooth. (Im sorry for the not really pragmatic opinion of mine. )

I use in my application an approach with DI. Which means:

router.route(HttpMethod.GET,
 "/user/login").handler(injector.getInstance(ILoginUser.class));

With normal guice framework as injector. And while this is just an Interface you can make really big changes before you have to change something in the verticle which starts the server. (Actually mostly just if you have to add or remove a path)

Summary:

  • If you want to have a Spring approach you have to use reflection or a library which uses reflection. Downside: Startup-performance, Sometimes a bit to much magic and really hard to find errors/debug. Upside: So easy to test, really really easy to extend functionality

  • Register the Verticles on the path urself. Downside: You have to add / remove paths on the "server"-verticle. Upside: Startup-performance, No magic, full control about what happens and when.

Thats just a short summary and many points aren't mentioned. But i hope this answers your questions. If you have some follwoup questions just write!

Jeerze,

Simi

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)