问题描述
这是我用来绘制折线图的代码.
Here's the code I use to plot a line graph.
var db_query = <?php echo json_encode($db_query_1) ?>;
var chart;
var graph;
/* chart initialization */
var chart = AmCharts.makeChart("plot1", {
type: "serial",
categoryField: "OCCUR_DATE",
graphs: [{
type: "smoothedLine",
theme: "light",
valueField: "FREQ"
}]
})
$.ajax({
type: 'POST',
url: "mySQL.php",
data: {'db_que': db_query},
dataType: 'html',
context: document.body,
global: false,
async:true,
success: function(data) {
//alert(data);
chart.dataProvider = eval(data);
chart.validateNow();
}
});
我愿意
- 创建一个单独的 javascript 文件,并为每种类型的图形和图形定义属性.导入那个 javascript 文件
替换为:
- Create a separate javascript file with properties defined for each of line, pie, bar etc types of graphs & import that javascript file
replace this:
var chart = AmCharts.makeChart("plot1", {
type: "serial",
categoryField: "OCCUR_DATE",
graphs: [{
type: "smoothedLine",
theme: "light",
valueField: "FREQ"
}]
})
与
var chart = AmCharts.makeChart("plot1", options_line_graph);
我希望能够将参数传递给x轴&y 轴标题 &图形名称.这些至少是非常我现在能想到的几个可变参数.如果我能做到这一点,我认为可以轻松添加更多可变参数.
I'd like to be able to pass parameters to the properties for thex-axis & y-axis titles & the graph name. These are at least the veryfew variable parameters I can think of now. If I can achieve this,adding more variable parameters could be done easily I presume.
我在这里寻求帮助的原因是,我有 30 多个折线图、20 个饼图和我将绘制的其他一些类型.在 & 上设置相同的属性集再次听起来是一种低效的做事方式.
The reason I ask for help here is, I have over 30 line graphs, 20 pie charts & a few of other types that I will plot. Setting the same set of properties over & over again sounds an inefficient way of doing things.
有人可以指导/帮助我吗?
Could someone please guide/help me?
推荐答案
图表实例重用了你作为配置传入的对象,所以,你自然不能将同一个对象传入多个图表.
The chart instance reuses the object you pass in as configuration, so, naturally, you cannot pass in the same object to multiple charts.
不过,您可以做的是在应用修改并传入图表实例之前传入重复的配置对象.
What you can do, though, is to pass in duplicated the config object before applying modifications and passing in to chart instance.
这个SO线程中有一个很好的对象克隆功能.
There's a good object cloning function in this SO thread.
所以基本上你可以这样做:
So basically you could do something like this:
// define universal config
var universalConfig = {...};
// clone universalConfig
var instanceConfig1 = clone(universalConfig);
// make modifications
instaceConfig1.categoryField = "OCCUR_DATE";
// create the chart
var chart = AmCharts.makeChart("plot1", instaceConfig1);
// repeat
// ...
这是一个 CodePen 上的工作示例.
这篇关于amchart - 将参数传递给属性/跨图形重用属性/跨多个图形使用相同的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!