cube icon indicating copy to clipboard operation
cube copied to clipboard

vuejs echarts example

Open devopsprosiva opened this issue 6 years ago • 5 comments

Hi @paveltiunov ,

I want to display data from cube-js using vuejs and echarts. Could you provide a sample working example? I'm specifically using vue-echarts package.

Here's my ChartRenderer.vue code. Right now it fails with computed value series should return a value.

<template>
  <div class="chart-renderer">
    <echart :options="series"></echart>
  </div>
</template>

<script>
import Echarts from 'vue-echarts';

import 'echarts/lib/chart/line'

// Load query for orders by created month in 2017 year
// cubejsApi
//   .load({
//       measures: ["Metrics.nodeMemUsagePercent"],
//       timeDimensions: [
//         {
//           dimension: 'Metrics.time',
//           granularity: 'minute',
//         },
//       ],
//       "filters":[]
//     })
//   .then(resultSet => {
//     // initialize echarts instance with prepared DOM
//     var myChart = echarts.init(document.getElementById('chart'));
//     // draw chart
//     myChart.setOption({
//       xAxis: {
//         data: resultSet.chartPivot().map(i => i.x)
//       },
//       yAxis: {},
//       series: [
//         {
//           type: 'bar',
//           data: resultSet.chartPivot().map(i => i['Orders.count'])
//         }
//       ]
//     });
//   });

export default {
  name: 'ChartRenderer',
  props: {
    resultSet: {
      type: Object,
      required: true,
    },
  },
  computed: {
    series() {
      // const seriesNames = this.resultSet.seriesNames();
      // const pivot = this.resultSet.chartPivot();
      // const series = [];
      // seriesNames.forEach((e) => {
      //   const data = pivot.map(p => [p.x, p[e.key]]);
      //   series.push({ name: e.key, data });
      // });
      resultSet => { 
        var myChart = Echarts.init(document.getElementById('chart'));
        myChart.setOption({
          xAxis: {
            data: resultSet.chartPivot().map(i => i.x)
          },
          yAxis: {},
          series: [
            {
              type: 'bar',
              data: resultSet.chartPivot().map(i => i['Orders.count'])
            }
          ]
        });
      }
      },
      
      // return series;
  
    },
};
</script>

devopsprosiva avatar Jan 20 '20 08:01 devopsprosiva

@devopsprosiva Have you checked this one: https://cube.dev/blog/vue-dashboard-tutorial-using-cubejs/?

paveltiunov avatar Jan 20 '20 23:01 paveltiunov

Hi @paveltiunov ,

Thanks for the reply. I was able to get it working with the Users.Count query from the example in https://cube.dev/blog/vue-dashboard-tutorial-using-cubejs.

However, when I tried with my own data with postgres backend, the ResultSet structure being passed to the ChartRenderer component is different from the ResultSet in the Users.Count example. As a result, the resultSet.chartPivot().map(i => i.x) call is displaying only one value in the x-axis.

I'd really appreciate if you could help me figure out why this is happening. I'm including the necessary data below.

Chart screenshot image

Here's the truncated resultSet from the Users.Count query.

{
  "loadResponse": {
    "query": {
      "measures": [
        "Users.count"
      ],
      "timeDimensions": [
        {
          "dimension": "Users.createdAt",
          "granularity": "day"
        }
      ],
      "filters": [],
      "timezone": "UTC",
      "dimensions": []
    },
    "data": [
      {
        "Users.createdAt.day": "2016-01-02T00:00:00.000",
        "Users.createdAt": "2016-01-02T00:00:00.000",
        "Users.count": "1"
      },
      {
        "Users.createdAt.day": "2016-01-06T00:00:00.000",
        "Users.createdAt": "2016-01-06T00:00:00.000",
        "Users.count": "1"
      },
      {
        "Users.createdAt.day": "2016-01-16T00:00:00.000",
        "Users.createdAt": "2016-01-16T00:00:00.000",
        "Users.count": "1"
      },
      {
        "Users.createdAt.day": "2016-02-17T00:00:00.000",
        "Users.createdAt": "2016-02-17T00:00:00.000",
        "Users.count": "1"
      }
    ],
    "lastRefreshTime": "2020-01-24T03:42:49.257Z",
    "refreshKeyValues": [
      [
        {
          "max": "2020-01-01T04:30:57.000"
        }
      ]
    ],
    "usedPreAggregations": {},
    "annotation": {
      "measures": {
        "Users.count": {
          "title": "Users Count",
          "shortTitle": "Count",
          "type": "number"
        }
      },
      "dimensions": {},
      "segments": {},
      "timeDimensions": {
        "Users.createdAt.day": {
          "title": "Users Created at",
          "shortTitle": "Created at",
          "type": "time"
        }
      }
    }
  }
}

Here's the truncated resultSet.chartPivot() output from Users.Count query.

[
  {
    "category": "2016-01-02T00:00:00.000",
    "x": "2016-01-02T00:00:00.000",
    "Users.count": 1
  },
  {
    "category": "2016-01-03T00:00:00.000",
    "x": "2016-01-03T00:00:00.000",
    "Users.count": 0
  },
  {
    "category": "2016-01-04T00:00:00.000",
    "x": "2016-01-04T00:00:00.000",
    "Users.count": 0
  }
]

Here's the truncated resultSet from my Metrics.UsagePercent query.

{
  "loadResponse": {
    "annotation": {
      "dimensions": {},
      "measures": {
        "Metrics.UsagePercent": {
          "format": "percent",
          "shortTitle": "Usage Percent",
          "title": "Metrics Usage Percent",
          "type": "number"
        }
      },
      "segments": {},
      "timeDimensions": {
        "Metrics.time": {
          "shortTitle": "Time",
          "title": "Metrics Time",
          "type": "time"
        }
      }
    },
    "data": [
      {
        "Metrics.UsagePercent": "18.65",
        "Metrics.time": "2020-01-12T21:41:00.000"
      },
      {
        "Metrics.UsagePercent": "19.89",
        "Metrics.time": "2020-01-12T21:42:00.000"
      },
      {
        "Metrics.UsagePercent": "24.69",
        "Metrics.time": "2020-01-12T21:43:00.000"
      }
    ],
    "lastRefreshTime": "2020-01-24T03:54:30.463Z",
    "query": {
      "dimensions": [],
      "filters": [],
      "measures": [
        "Metrics.UsagePercent"
      ],
      "timeDimensions": [
        {
          "dimension": "Metrics.time",
          "granularity": "minute"
        }
      ],
      "timezone": "UTC"
    },
    "usedPreAggregations": {}
  }
}

Here's the full resultSet.ChartPivot() output from my Metrics.UsagePercent query.

[
  {
    "category": "∅",
    "x": "∅",
    "Metrics.nodeMemUsagePercent": 24.69
  }
]

devopsprosiva avatar Jan 25 '20 05:01 devopsprosiva

This is my schema definition.

cube(`test`, {
  sql: `SELECT * FROM test`,

  joins: {

  },

  measures: {
    count: {
      type: `count`,
      drillMembers: [name]
    },

    value: {
      sql: `value`,
      type: `avg`
    },

    UsageAvg: {
      sql: `value`,
      type: `avg`,
      filters: []
    },

    UsagePercent: {
      sql: `round(cast(${UsageAvg} * 100 as numeric), 2)`,
      type: `number`,
      format: `percent`
    }

  },

  dimensions: {
    labels: {
      sql: `labels`,
      type: `string`
    },

    sample: {
      sql: `sample`,
      type: `string`
    },

    name: {
      sql: `name`,
      type: `string`
    },

    time: {
      sql: `time`,
      type: `time`
    }
  }
});

devopsprosiva avatar Jan 25 '20 05:01 devopsprosiva

@devopsprosiva I can confirm that there was backward compatibility bug: newer version of client core wasn't working with older server. Just fixed it.

Could you please update client core and check? Thanks for heads up!

paveltiunov avatar Jan 25 '20 20:01 paveltiunov

@paveltiunov Thanks for the quick fix 👏 . It's working now.

devopsprosiva avatar Jan 26 '20 04:01 devopsprosiva