将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
最满意答案
你的需要是绝对可以理解的。 但是我们应该很快就会想到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
相关问答
更多-
我不建议写入具有许多不同“编写者”的单个文件。 关于并发日志记录,我会坚持使用Single Writer原则。 创建一个订阅事件总线的Verticle并侦听要记录的消息。 让我们调用这个侦听system.logger Verticle Logger 。 EventBus eb = vertx.eventBus(); eb.consumer("system.logger", message -> { // write to file }); 喜欢Logger Verticle需要向Logger Ver ...
-
据我所知,没有。 但是,您可以对config.json和deploy(Class, DeploymentOptions)方法进行一些调整,以获得类似的结果。 对于config.json ,如果将每个Verticle的名称更改为限定类名并为每个Verticle创建一个deployment_options对象,则可以修改deploy()以加载选项,而无需在start方法中指定它们。 在Vert.x中,您可以提供默认配置选项,因此您可以执行以下操作: override fun start(future: Futu ...
-
使用PlatformManager :它允许您以编程方式部署Verticle。 Use the PlatformManager: it allows you to deploy verticles programmatically.
-
我们把它分成两部分: 如何取消部署Verticle 如何在业务逻辑和VertX之间进行沟通 这是一个Verticle 5秒后取消部署的例子。 class StoppingVerticle extends AbstractVerticle { @Override public void start() { System.out.println("Starting"); vertx.setTimer(TimeUnit.SECONDS.toMillis(5), ...
-
Vert.X与Hazelcast(Vert.X with Hazelcast)[2023-12-10]
我真的认为你应该尝试不同的方法,否则你将参与Vert.x试图缓解的最强点之一:并发问题。 如果我有这个要求,我会使用类似Redis的东西,拥有一个非常快速,集中,易变的商店,我可以访问和分享一些东西 。 也许这不能回答你的问题,但指出了一种不同的方法......无论如何,试着远离“共享状态”。 祝你好运! I really think you should try a different approach, otherwise you will be involved in one of the stron ... -
由于一段时间没有答案,我按照我最初在问题中描述的方式实现了它。 它实际上很顺利,演示代码可以在这里找到: https : //github.com/michalboska/codingbeer-vertx 有一个Verticle(GameLobbyVerticle)为每个游戏启动GameVerticle的新实例。 GameVerticle实例记住(在成员变量中)与特定游戏实例相关的所有状态。 每个实例还创建一些EventBus端点(地址包含唯一的游戏ID,每个GameVerticle实例都有自己独特的eve ...
-
我必须更新我的java参数进行聚类: 运行com.xxx.MainVerticle --launcher-class = io.vertx.core.Launcher --conf = xxx.json -Dvertx.options.blockedThreadCheckInterval = 200000000 -Dfile.encoding = UTF-8 -cp。 -簇 I have to update my java arguments for clustering : run com.xxx.Mai ...
-
Vert.x RESTful Verticle(Vert.x RESTful Verticle)[2023-01-15]
你的需要是绝对可以理解的。 但是我们应该很快就会想到Spring会做什么: - 当应用服务器启动时,执行startuphook,它会搜索整个类路径,查找用Jax-rs Annotations分配的每个类,并将它们初始化或只是在“路由器”上注册它们。 所以,如果你想要那样,你就可以拥有它,但你必须通过自己做到这一点。 对不起:D。 例如: class Server extends AbstractVerticle { @Override public void start() throws ... -
首先,您需要实现start(Future future)而不仅仅是start() 。 然后你需要在listen(...)调用中添加一个回调处理程序( Handler
> listenHandler ) - 然后解析你通过start(Future future)获得的start(Future future) 。 Vert.x是高度异步的 - Vert.x HTTP服务器的启动也是如此。 在您的情况下,当HTTP服务器成功启动时,Verticle将完全正常运行。 因 ... -
简答:不。 更长的答案:仍然没有:-)您可以开始使用vert.x开发而不使用Verticle。 为此,您可以在常规Java应用程序中使用处理程序和期货的主要概念。 Verticle的概念在以下情况下特别有用: 多语言编程 - 每个Verticle都可以用另一种编程语言开发 缩放 - 如果你有一些包含逻辑需要更长时间或更频繁调用的Verticle,那么你可以部署更多的verticle实例 分布式应用程序(又名“微服务”) - 当您想在不同的机器上部署Verticle时。 有关实际项目的更多信息可以在新蓝图教程 ...