Chart.js:如何创建包含多个JSON文件的图形?

时间:2018-10-01 13:27:56

标签: chart.js

我有20个jsons文件。每个文件中都有12个标记:

{"mean":0.08614979321142166,
 "min":-3.1884,
 "max":2.1901,
 "peak2peak":5.3785000000000007,
 "std":1.0903240544512534,
 "variance":1.1888065437150195,
 "kurtosis":2.165975182739587,
 "skewness":-0.041289626007299074,
 "rmsOriginalSignal":1.0937041158972693,
 "rmsFiltSignal_01":1.0922792414296567,
 "rmsFiltSignal_02":0.00036638538212209666,
 "rmsFiltSignal_03":0.032341062844535272}

我必须创建一个带有均值的图形:

mean

如何从本地文件夹加载此外部json文件?  如何从每个文件中获取每个均值并创建均值图?

1 个答案:

答案 0 :(得分:0)

据我了解,在您的json文件中,结构如下。

var jsonObjectArray = [{"mean":0.08614979321142166, "min":-3.1884, "max":2.1901, "peak2peak":5.3785000000000007, "std":1.0903240544512534, "variance":1.1888065437150195, "kurtosis":2.165975182739587, "skewness":-0.041289626007299074, "rmsOriginalSignal":1.0937041158972693, "rmsFiltSignal_01":1.0922792414296567, "rmsFiltSignal_02":0.00036638538212209666, "rmsFiltSignal_03":0.032341062844535272}, {"mean":0.08614979321142166, "min":-3.1884, "max":2.1901, "peak2peak":5.3785000000000007, "std":1.0903240544512534, "variance":1.1888065437150195, "kurtosis":2.165975182739587, "skewness":-0.041289626007299074, "rmsOriginalSignal":1.0937041158972693, "rmsFiltSignal_01":1.0922792414296567, "rmsFiltSignal_02":0.00036638538212209666, "rmsFiltSignal_03":0.032341062844535272}, ....]

您可以使用loadash.js映射函数来修饰json对象数组中的属性。

_.map(jsonObjectArray, "mean");

然后您将得到[0.08614979321142166, 0.18614979321142186]

例如,如果您尝试使用所有这些数据构建网页,则将包括.js文件之类的20个不同的html文件,或者将获得如下所示的json文件,

<script>

var jsonArray = null;
for(i=0; i<19 ; i++){
   $.getJSON('mean' + i + '.json', function(data) {
      jsonArray.push(data);
   });
}
<script/>`

我将您的20个json文件命名为mean1.json,mean2.json ...等。