CubeJS Service

A Dart package to interact with the Cube.js API and fetch data for charts, with support for converting the data into FlSpot objects for use in Flutter's fl_chart.

Features

  • Fetch raw data from the Cube.js API.
  • Convert Cube.js data into FlSpot for use with fl_chart.
  • Simple and easy-to-use API.

Installation

Add the following to your pubspec.yaml:

dependencies:
  cubejs_service: ^0.0.1
  http: ^0.13.0
  fl_chart: ^0.40.0

Then run:

flutter pub get

Usage

1. Initialize the CubeJS Service

import 'package:cubejs_service/cubejs_service.dart';

void main() {
  CubeJSService cubeService = CubeJSService(
    cubeToken: 'your_cube_token',
    baseUrl: 'api.cubecloud.dev',
  );
}

2. Fetch Data from Cube.js

To fetch raw data from the Cube.js API, pass your query to fetchRawData:

final query = {
  "measures": ["Orders.count"],
  "timeDimensions": [
    {
      "dimension": "Orders.createdAt",
      "dateRange": "Last 30 days"
    }
  ]
};

final rawData = await cubeService.fetchRawData(query);
print(rawData);

3. Convert to FlSpot for fl_chart

You can convert the fetched data into FlSpot data points for use with fl_chart:

List<FlSpot> flSpots = cubeService.convertToFlData(
  List<Map<String, dynamic>>.from(rawData),
  'Orders.createdAt',
  'Orders.count',
);

print(flSpots);

Example

Here’s an example that fetches data from Cube.js and converts it into a format suitable for fl_chart:

import 'package:cubejs_service/cubejs_service.dart';
import 'package:fl_chart/fl_chart.dart';

void main() async {
  CubeJSService cubeService = CubeJSService(
    cubeToken: 'your_cube_token',
    baseUrl: 'api.cubecloud.dev',
  );

  final query = {
    "measures": ["Orders.count"],
    "timeDimensions": [
      {
        "dimension": "Orders.createdAt",
        "dateRange": "Last 30 days"
      }
    ]
  };

  try {
    final rawData = await cubeService.fetchRawData(query);
    final flSpots = cubeService.convertToFlData(
      List<Map<String, dynamic>>.from(rawData),
      'Orders.createdAt',
      'Orders.count',
    );

    // Use flSpots in your chart
    print(flSpots);
  } catch (e) {
    print('Error: $e');
  }
}

License

This project is licensed under the MIT License - see the LICENSE file for details.

Libraries

cubejs_service