CanvasJS中的动态堆积列

时间:2019-10-14 11:48:47

标签: javascript json canvasjs

如何创建包含动态数据的堆积柱形图?

例如,我有一个外部JSON对象,例如:

[
  {
    id : ‘ID123’,
    occurrences: [5,6,8],
    description: ["Hours spent working", "Hours spent skiing", "Hours spent studying"]
  },
  {
    id : ‘ID456’,
    occurrences: [7,2,12],
    description: ["Hours spent working", "Hours spent skiing", "Hours spent studying"]
  }
]

我如何做到这一点,所以将有两个使用id作为标签的堆叠列,并且使用出现的数组在每个标签的顶部构建堆叠列?

编辑: 添加了图形外观的表示形式

Representation of graph

1 个答案:

答案 0 :(得分:2)

我建议为此使用chart.js,因为这比自己构建它要容易得多。这是使用您提供的数据的示例。

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js"></script>
<canvas id="canvas"></canvas>
Open Application  ${REMOTE_URL}  platformName=${PLATFORM_NAME}  platformVersion=${PLATFORM_VERSION}  deviceName=${DEVICE_NAME}  appPackage=${${ENV}_APP_PACKAGE}  automationName=UiAutomator2  app=${${ENV}_APP}  appActivity=com.dummyfi.viestit.ui.splash.SplashActivity     noReset=True