如何在高图表中设置字段名?

时间:2014-09-17 20:13:04

标签: javascript highcharts

我是highcharts的新手。我之前使用过Kendo UI图表。在剑道,我们可以告诉我们想要在图表中绘制的字段名称如下:

series: [{
    name: "steps",
    field: "steps",
    categoryField: "createddate"
}]

我们可以告诉dataSource如下:

dataSource: dSource

其中dSource是一个AJAX URL。

我没有在教程中的任何地方找到这样的东西。我的JSON文件如下:

[{"ActivitySummaryKey":174000,
    "id":"kkse2",
    "activityscore":-,
    "activitycalories":456,
    "caloriesBMR":1017,
    "caloriesOut":1412,
    "distances":1.57828236,
    "elevation":0,
    "fairlyActiveminutes":34,
    "floors":0,
    "lightlyActiveMinutes":28,
    "marginalCalories":334,
    "sedentaryMinutes":827,
    "steps":5077,
    "veryActiveMinutes":26,
    "trackersteps":0,
    "trackerdistances":0,
    "trackerfloors":0,
    "trackerelevation":0,
    "trackerActivityCalories":0,
    "trackerCaloriesOut":0,
    "trackerMinutesSedentary":0,
    "trackerminutesLightlyActive":0,
    "trackerminutesFairlyActive":0,
    "trackerminutesVeryActive":0,
    "createddate":"9/17/2014 12:00:00 AM",
    "distanceunit":"Miles"
}]

我想在y轴上绘制步骤并在x轴上创建日期。

如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您应该使用高图表数据格式 - > series.data

要使您的点可见,必须使用JSON(steps和createddate)中的值设置x和y。 您还需要解析日期(例如Date.UTC())。

因此,Highcharts所需的结果将是:

[{
    "ActivitySummaryKey":174000,
    "id":"kkse2",
    "activityscore":-,
    "activitycalories":456,
    "caloriesBMR":1017,
    "caloriesOut":1412,
    "distances":1.57828236,
    "elevation":0,
    "fairlyActiveminutes":34,
    "floors":0,
    "lightlyActiveMinutes":28,
    "marginalCalories":334,
    "sedentaryMinutes":827,
    "steps":5077,
    "veryActiveMinutes":26,
    "trackersteps":0,
    "trackerdistances":0,
    "trackerfloors":0,
    "trackerelevation":0,
    "trackerActivityCalories":0,
    "trackerCaloriesOut":0,
    "trackerMinutesSedentary":0,
    "trackerminutesLightlyActive":0,
    "trackerminutesFairlyActive":0,
    "trackerminutesVeryActive":0,
    "createddate":"9/17/2014 12:00:00 AM",
    "distanceunit":"Miles",
    "x": 1410904800000,      // timestamp in ms for Highcharts, extracted from "createdate"
    "y": 5077                // y-value for Highcharts
}]