cube icon indicating copy to clipboard operation
cube copied to clipboard

echarts stacked resultset with a dimension

Open devopsprosiva opened this issue 6 years ago • 6 comments

Hi @paveltiunov ,

In the official example for the vanilla js client https://codesandbox.io/s/131ymrj8vl?from-embed, I want to add the Orders.status as a dimension and then stack resulting Orders.count measure based on the status.

In this scenario, how should the xaxis data param be modified to take into account the new dimension Orders.status?

xAxis: {
  data: resultSet.chartPivot().map(i => i.x)
},

I want to replicate the Orders by Status Over Time chart from the reach dashboard demo https://react-dashboard-demo.cube.dev/#/ in echarts.

image

Thanks, Siva

devopsprosiva avatar May 26 '20 17:05 devopsprosiva

Hi, @devopsprosiva

You don't need to modify xAxis but you need to provide a pivotConfig to the chartPivot and series methods

const pivotConfig = {
  x: ['Orders.createdAt.month'],
  y: ['Orders.status', 'measures'],
};

and then you would be able to adjust the result for echarts

const echartsSeries = resultSet.series(pivotConfig).map((item) => {
  return {
    name: item.title,
    type: 'bar',
    stack: 'statusStack',
    data: item.series.map(itemSeries => itemSeries.value)
  }
})

vasilev-alex avatar May 27 '20 10:05 vasilev-alex

Hi @vasilev-alex ,

Thanks for the suggestion. I made changes to the sandbox code as below, but I don't see any chart data? Could you please check and advise?

import echarts from "echarts";
import cubejs from "@cubejs-client/core";

// initialize cubejs instance with API Token and API URL
const cubejsApi = cubejs(
  "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.K9PiJkjegbhnw4Ca5pPlkTmZihoOm42w8bja9Qs2qJg",
  { apiUrl: "https://react-query-builder.herokuapp.com/cubejs-api/v1" }
);

// Load query for orders by created month in 2017 year
cubejsApi
  .load({
    measures: ["Orders.count"],
    timeDimensions: [
      {
        dimension: "Orders.createdAt",
        dateRange: ["2017-01-01", "2017-12-31"],
        granularity: "month"
      }
    ],
    dimensions: ["Orders.status"]
  })
  .then(resultSet => {
    // initialize echarts instance with prepared DOM
    var myChart = echarts.init(document.getElementById("chart"));

    const pivotConfig = {
      x: ['Orders.createdAt.month'],
      y: ['Orders.status', 'measures'],
    };
    
    const echartsSeries = resultSet.series(pivotConfig).map((item) => {
      return {
        name: item.title,
        type: 'bar',
        stack: 'statusStack',
        data: item.series.map(itemSeries => itemSeries.value)
      }
    })
    // draw chart
    myChart.setOption({
      xAxis: {
        data: resultSet.chartPivot().map(i => i.x)
      },
      yAxis: {},
      series: [
        {
          type: "bar",
          data: echartsSeries.data
        }
      ]
    });
  });

devopsprosiva avatar May 27 '20 11:05 devopsprosiva

Please see the working example here https://codesandbox.io/s/cubejs-vanilla-javascript-client-0twnw

I also had to update @cubejs-client/core to the latest version as the one in the sandbox was too outdated.

vasilev-alex avatar May 27 '20 11:05 vasilev-alex

@vasilev-alex Thanks for the new example.

Could you let me know how I can implement this in vuejs?

devopsprosiva avatar Sep 07 '20 17:09 devopsprosiva

@Levhav Could you please take a look and let me know how to get this working in vuejs?

devopsprosiva avatar Sep 08 '20 09:09 devopsprosiva

Could someone help me do this with React?

Vando-Lopes avatar May 28 '21 03:05 Vando-Lopes