gpt4 book ai didi

javascript - 如何在amCharts中使用dataLoader获取数据?

转载 作者:行者123 更新时间:2023-11-30 16:40:21 25 4
gpt4 key购买 nike

我正在使用 amCharts 库(它是一个用于在图表中加载数据的 JavaScript 库)从 mySQL 数据库中提取数据,并使用股票图表显示它。

我在这里找到了完整的教程:

http://www.amcharts.com/tutorials/your-first-stock-chart/

他们使用他们在代码中创建的静态数据,但是我想通过从数据库中提取 JSON 格式的数据来使用我自己的数据。

要包含的 PHP 文件:json_encode.php

<?php
include("data.php");
echo json_encode($rows);
?>

这个文件的输出是这样的:

[{"date":"2014-01-19 00:00:00","value":"15"},{"date":"2014-04-19 00:00:00","value":"8"},{"date":"2014-05-19 00:00:00","value":"54"},{"date":"2014-07-19 00:00:00","value":"5"},{"date":"2014-09-19 00:00:00","value":"24"},{"date":"2014-11-19 00:00:00","value":"18"},{"date":"2015-01-19 00:00:00","value":"36"},{"date":"2015-02-19 00:00:00","value":"10"},{"date":"2015-03-11 16:54:55","value":"7"},{"date":"2015-03-19 00:00:00","value":"45"},{"date":"2015-04-19 00:00:00","value":"17"},{"date":"2015-05-19 00:00:00","value":"4"},{"date":"2015-06-19 00:00:00","value":"27"}]

教程中的完整代码:

<script type="text/javascript">

var chartData= [
{date: new Date(2011, 5, 1, 0, 0, 0, 0), val:10},
{date: new Date(2011, 5, 2, 0, 0, 0, 0), val:11},
{date: new Date(2011, 5, 3, 0, 0, 0, 0), val:12},
{date: new Date(2011, 5, 4, 0, 0, 0, 0), val:11},
{date: new Date(2011, 5, 5, 0, 0, 0, 0), val:10},
{date: new Date(2011, 5, 6, 0, 0, 0, 0), val:11},
{date: new Date(2011, 5, 7, 0, 0, 0, 0), val:13},
{date: new Date(2011, 5, 8, 0, 0, 0, 0), val:14},
{date: new Date(2011, 5, 9, 0, 0, 0, 0), val:17},
{date: new Date(2011, 5, 10, 0, 0, 0, 0), val:13}
];

AmCharts.ready(function() {
var chart = new AmCharts.AmStockChart();
chart.pathToImages = "amcharts/images/";

var dataSet = new AmCharts.DataSet();
dataSet.dataProvider = chartData;
dataSet.fieldMappings = [{fromField:"val", toField:"value"}];
dataSet.categoryField = "date";
chart.dataSets = [dataSet];

var stockPanel = new AmCharts.StockPanel();
chart.panels = [stockPanel];

var legend = new AmCharts.StockLegend();
stockPanel.stockLegend = legend;

var panelsSettings = new AmCharts.PanelsSettings();
panelsSettings.startDuration = 1;
chart.panelsSettings = panelsSettings;

var graph = new AmCharts.StockGraph();
graph.valueField = "value";
graph.type = "column";
graph.title = "MyGraph";
graph.fillAlphas = 1;
stockPanel.addStockGraph(graph);

var categoryAxesSettings = new AmCharts.CategoryAxesSettings();
categoryAxesSettings.dashLength = 5;
chart.categoryAxesSettings = categoryAxesSettings;

var valueAxesSettings = new AmCharts.ValueAxesSettings();
valueAxesSettings .dashLength = 5;
chart.valueAxesSettings = valueAxesSettings;

var chartScrollbarSettings = new AmCharts.ChartScrollbarSettings();
chartScrollbarSettings.graph = graph;
chartScrollbarSettings.graphType = "line";
chart.chartScrollbarSettings = chartScrollbarSettings;

var chartCursorSettings = new AmCharts.ChartCursorSettings();
chartCursorSettings.valueBalloonsEnabled = true;
chart.chartCursorSettings = chartCursorSettings;

var periodSelector = new AmCharts.PeriodSelector();
periodSelector.periods = [{period:"DD", count:1, label:"1 day"},
{period:"DD", selected:true, count:5, label:"5 days"},
{period:"MM", count:1, label:"1 month"},
{period:"YYYY", count:1, label:"1 year"},
{period:"YTD", label:"YTD"},
{period:"MAX", label:"MAX"}];
chart.periodSelector = periodSelector;

chart.write("chartdiv");
});

所以代替这一行:

dataSet.dataProvider = chartData;

我想使用 dataLoader 以 JSON 格式从 PHP 加载数据:

dataSet.dataloader =  {
"url": "json_encode.php"
},

可能dataLoader的声明不正确,因为它不起作用。

任何帮助将不胜感激:)

最佳答案

虽然有点晚了,以防万一有人遇到这个问题......

按照 AmCharts 给出的 DataLoader 教程,所有步骤都描述清楚,甚至还有股票图表的示例。 https://www.amcharts.com/tutorials/using-data-loader-plugin/

关于javascript - 如何在amCharts中使用dataLoader获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32105687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com