• Stars
    star
    222
  • Rank 173,181 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 4 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

graphql2chartjs reshapes your GraphQL data as per the ChartJS API.

graphql2chartjs - Instant realtime charts using GraphQL

graphql2chartjs reshapes your GraphQL data as per the ChartJS API. This makes it easy to query a GraphQL API and render the output as a ChartJS chart.

For example, if you're using Postgres and Hasura, this is what using graphql2chartjs looks like:

graphql2chartjs

Demos & sandbox

We've set up a GraphQL server with continuously changing data, so that you can try graphql2chartjs out easily.

View live charts Edit in sandbox Open GraphiQL

realtime chart with live data

The demo above cover the following types of charts: basic, multiple datasets, mixed chart-types, realtime chart with live data, realtime time-series

Usage with Hasura

Hasura gives you an instant realtime GraphQL API on an existing Postgres database. You can create views to capture analytics and aggregations on your database and instantly turn them into charts.

Watch this video below to see a demo/tutorial of using Hasura with an existing Postgres database, creating views and building charts.

Example usage with react, apollo and react-chartjs-2

import {Query} from 'react-apollo';
import gql from 'graphql-tag';
import graphql2chartjs from 'graphql2chartjs';
import {Bar} from 'react-chartjs-2';

const Chart = () => (
  <Query
    query={gql`
      query {
        Articles: articleStats {
          label: title
          data: num_likes
        }
      }`}
    }>
    {({data} => {
      if (data) {
        const g2c = new graphql2chartjs(data, 'bar');
        return (<Bar data={g2c.data} />);
      }
      return null;
    }
  </Query>
);

Mapping GraphQL queries to ChartJS charts

Different types of charts need different structures in their datasets.

For example a bar chart dataset needs labels and data associated for each label; the ChartJS API refers to this as label and data. Once you alias fields in your graphql query to label and data, and pass the response through graphql2chartjs, your dataset is ready to be used by bar chart in chartjs.

Bar / Line / Doughnut / Pie / Radar / Polar Area / Area

Charts of this type need 2 data inputs, label and data.

query {
  ArticleLikes : articles {
    label: title
    data: likes
  }
}

Scatter / Bubble

Charts of this type need 2 data inputs: data_x, data_y (and data_r for bubble).

query {
  ArticleLikesVsComments : articles {
    data_x: num_likes
    data_y: num_comments
  }
}

Time series (line / bar)

Charts of this type need 2 data inputs, data_x or data_t and data_y. Note that there is no label.

query {
  StockPrices : stockprice {
    data_t: created
    data_y: price
  }
}

graphql2chartjs usage

graphql2chartjs works in 3 steps:

  1. Initialise graphql2chartjs: const g2c = new graphql2chartjs()
  2. Add data from your graphql response: g2c.add(graphqlResponse.data, 'line')
  3. Set your chart data to the data properly of the graphql2chartjs instance: g2c.data

Step 1: Initialiase with data: new graphql2chartjs()

Option 1: Initialise with data and chart type

graphql2chartjs(data, chartType)

const g2c = new graphql2chartjs(data, 'bar');
  • data: This is your GraphQL response. This data should have fields label, data etc. as per the GraphQL querying described above.
  • chartType: This is a string that represents valid values of what your chart type is. Valid values include 'line', 'bar', 'radar', 'doughnut', 'pie', 'polarArea', 'bubble', 'scatter'.

Notes:

  • This is the simplest way of using graphql2chartjs
  • If you have multiple datasets, all of the datasets will be rendered automatically as the same type of chart
  • To customise the UI options of the rendered chart like colors or to create a mixed type chart (one dataset is rendered as a line chart, another as a bar chart) use the next initialisation method instead of this one.

Option 2: Initialise with data and a transform function

graphql2chartjs(data, transform)

The transformation function can add chartjs dataset props or even modify the record data:

const g2c = new graphql2chartjs(data, (datasetName, dataPoint) => {
  return {
    chartType: 'bar',
    backgroundColor: 'yellow'
  };
});

Step 2: Now create your chart with data - g2c.data

g2c.data gives you access to the latest ChartJS data that can be passed to your chart.

  1. Javascript
var myChart = new Chart(ctx, { data: g2c.data });
  1. react-chartjs-2
<Bar data={g2c.data} />

Step 3: (optional) Incrementally add data for your chart

g2c.add()

Once you've initialised a graphql2chartjs object, you can use the add function to add data for the first time or incrementally:

await data = runQuery(..);

// Add for a chart type
g2c.add(data, 'line');

// Add with a transformation function to change UI props for the new data added or udpated
g2c.add(data, (datasetName, dataPoint) => {
  chartType: 'line',
  pointBackgroundColor: 'yellow'
});

Installation

Via npm

npm install --save graphql2chartjs

Use in a script tag

<script src="https://storage.googleapis.com/graphql-engine-cdn.hasura.io/tools/graphql2chartjs/index.js" type="application/javascript"></script>

Reforming the data

reform()

You can reform the existing data in your graphql2chartjs instance using the reform function that takes a reformer function as an argument. This reformer function is run over every datapoint in every dataset. For instance, to scale the x and y coordinates, you would do something like:

g2c.reform((datasetName, dataPoint) => {
  // scale the x, y coordinates
  return {
    data_x: scalingFactor(dataPoint.data_x),
    data_y: scalingFactor(dataPoint.data_y)
  }
})

More Repositories

1

graphql-engine

Blazing fast, instant realtime GraphQL APIs on your DB with fine grained access control, also trigger webhooks on database events.
TypeScript
30,844
star
2

gitkube

Build and deploy docker images to Kubernetes using git push
Go
3,758
star
3

graphqurl

curl for GraphQL with autocomplete, subscriptions and GraphiQL. Also a dead-simple universal javascript GraphQL client.
JavaScript
3,301
star
4

skor

Now part of Hasura GraphQL Engine. Listen to postgres events and forward them as JSON payloads to a webhook
C
1,247
star
5

learn-graphql

Real world GraphQL tutorials for frontend developers with deadlines!
JavaScript
1,121
star
6

gatsby-gitbook-starter

Generate GitBook style modern docs/tutorial websites using Gatsby + MDX
JavaScript
976
star
7

awesome-react-graphql

A curated collection of resources, clients and tools that make working with `GraphQL and React/React Native` awesome
735
star
8

react-check-auth

Add auth protection anywhere in your react/react-native app
JavaScript
530
star
9

eff

🚧 a work in progress effect system for Haskell 🚧
Haskell
530
star
10

3factor-example

Canonical example of building a 3factor app : a food ordering application
JavaScript
455
star
11

awesome-live-reloading

A curated collection of live-reloading / hot-reloading / watch-reloading tools for different languages and frameworks.
435
star
12

ra-data-hasura

react-admin data provider for Hasura GraphQL Engine
TypeScript
335
star
13

awesome-vue-graphql

A curated collection of resources, clients and tools that make working with `GraphQL and Vue.js` awesome
302
star
14

graphql-bench

A super simple tool to benchmark GraphQL queries
TSQL
256
star
15

hasura-ecommerce

TypeScript
245
star
16

pgdeltastream

Streaming Postgres logical replication changes atleast-once over websockets
Go
244
star
17

graphql-engine-heroku

Blazing fast, instant realtime GraphQL APIs on Postgres with fine grained access control, also trigger webhooks on database events.
Dockerfile
229
star
18

hasura-aws-stack

A complete production ready 100% serverless stack on AWS with Hasura
JavaScript
212
star
19

json2graphql

From a JSON file to postgres-backed realtime GraphQL
JavaScript
199
star
20

3factor

3factor app is an architecture pattern for modern fullstack apps. 3factor apps are fast to build and are highly scalable.
SCSS
179
star
21

client-side-graphql

147
star
22

hasura-k8s-stack

A feature-complete Hasura stack on Kubernetes
JavaScript
138
star
23

hasura-actions-examples

Examples of handling custom business logic with Hasura Actions
JavaScript
135
star
24

awesome-angular-graphql

A curated collection of resources, clients and tools that make working with `GraphQL and Angular` awesome
132
star
25

gqless-movies-demo

A movies app using Hasura and gqless
TypeScript
127
star
26

awesome-fluent-graphql

Awesome list of fluent GraphQL clients & examples
TypeScript
103
star
27

graphiql-online

Explore your GraphQL APIs with headers
JavaScript
88
star
28

kubeformation

Create declarative cluster specifications for your managed Kubernetes vendor (GKE, AKS)
Go
86
star
29

data-dictionary

TypeScript
83
star
30

firebase2graphql

Move from Firebase realtime db to instant GraphQL APIs on Postgres
JavaScript
81
star
31

jwt-guide

TypeScript
79
star
32

nodejs-graphql-subscriptions-boilerplate

Boilerplate to setup GraphQL subscriptions in your nodejs code
JavaScript
78
star
33

graphql-serverless

Example boilerplates for GraphQL backends hosted on serverless platforms
Go
70
star
34

graphql-parser-hs

A GraphQL query parser for Haskell
Haskell
59
star
35

sphinx-graphiql

Sphinx plugin that adds a GraphiQL directive so that you can embed an interactive GraphQL query explorer in your docs
JavaScript
57
star
36

kriti-lang

A minimal JSON templating language
Haskell
53
star
37

schema-stitching-examples

JavaScript
44
star
38

gitkube-example

An example repo to be used with gitkube: git push to deploy on to Kubernetes
HTML
43
star
39

graphql-backend-benchmarks

GraphQL performance benchmarks across Hasura, Postgraphile and Prisma
Shell
42
star
40

comment-progress

Notify progress by commenting on GitHub issues, pull requests, and commits :octocat: 💬
JavaScript
38
star
41

local-development

[Deprecated] Run Hasura locally on your computer
37
star
42

rxdb-hasura-demo

An Offline first todo app
JavaScript
37
star
43

monad-validate

(NOTE: REPOSITORY MOVED TO NEW OWNER: https://github.com/lexi-lambda/monad-validate) A Haskell monad transformer library for data validation
Haskell
32
star
44

pod42

Python
31
star
45

gitlab-graphql

Install gitlab and expose the gitlab api's over GraphQL
JavaScript
29
star
46

codegen-assets

TypeScript
28
star
47

data-hub

Explore data sources from a native GraphQL API, database schemas to custom code contributed by the community.
PLpgSQL
27
star
48

pg-client-hs

A low level Haskell library to connect to postgres
Haskell
25
star
49

template-gallery

Repository containing schema sharing packages.
PLpgSQL
24
star
50

yelp-clone-react

A Yelp clone built using React + GraphQL + Hasura
JavaScript
24
star
51

authz-workshop

TSQL
23
star
52

ndc-hub

Shell
22
star
53

graphql-schema-stitching-demo

Schema Stitching Example with Hasura GraphQL + MetaWeather API
JavaScript
22
star
54

github-integration-starter

Try out Hasura's GitHub Integration on Cloud Projects using the examples in this repo.
22
star
55

ndc-typescript-deno

Instant Hasura Native Data Connector by writing Typescript Functions
TypeScript
22
star
56

hasura-cloud-preview-apps

TypeScript
21
star
57

issues

Dump and sync org wide issues into postgres and visualise with metabase.
Python
19
star
58

imad-app

Base repository for IMAD course application.
JavaScript
19
star
59

realm-pg-sync

The realm-pg-sync microservice
JavaScript
18
star
60

graphql-data-specification

A specification for Data APIs with GraphQL
Haskell
18
star
61

hasura-discord-docs-bot

PLpgSQL
18
star
62

react-apollo-todo

A todo app with react, apollo demonstrating graphql queries, mutations and subscriptions.
CSS
17
star
63

architect-graphql-workshop

JavaScript
16
star
64

continuous-backup

Postgres wal-e continuous backup system
Shell
16
star
65

preview-actions

Starter kit to try out actions
JavaScript
16
star
66

auth-ui-kit

Web UI Kit for Hasura Authentication
JavaScript
15
star
67

graphql-example-apps

PLpgSQL
14
star
68

js-sdk

JavaScript
14
star
69

ndc-spec

NDC Specification and Reference Implementation
Rust
14
star
70

cloud-functions-boilerplates

Boilerplates for cloud functions (AWS Lambda, Google Cloud Functions, Azure Cloud Functions, Zeit, etc.) that work in conjunction with Hasura GraphQL Engine's event triggers
JavaScript
14
star
71

graphql-subscriptions-benchmark

TypeScript
13
star
72

sample-apps

TypeScript
12
star
73

smooth-checkout-buildkite-plugin

All the things you need during a Buildkite checkout 🧈 🪁
Shell
12
star
74

sqlite-dataconnector-agent

SQLite Data Connector Agent for Hasura GQL Engine. Please note that this repository is a mirror. We will still accept PRs, but will have to mirror them to our upstream repo.
TypeScript
11
star
75

github-bot

Hasura's own GitHub bot 🤖
JavaScript
11
star
76

generator-hasura-web

JavaScript
11
star
77

demo-apps

Config to deploy Hasura demo apps using Docker Compose
HTML
11
star
78

smooth-secrets-buildkite-plugin

A buildkite plugin to setup ssh keys and env secrets for your pipelines 🧈 🔒
Shell
11
star
79

chat-app-android

Java
10
star
80

custom-resolvers-boilerplate

A boilerplate for writing custom resolvers with Hasura GraphQL Engine
JavaScript
10
star
81

open-data-domain-specification

Rust
10
star
82

android-sdk

The Android SDK for Hasura
Java
9
star
83

sample-auth-webhook

Sample auth webhooks for the Hasura GraphQL engine
JavaScript
9
star
84

generator-hasura-node

JavaScript
9
star
85

graphql-asia-workshop

JavaScript
9
star
86

reactathon-workshop

9
star
87

go-buildkite-dsl

Write Buildkite configs in Go 🪁 📝
Go
8
star
88

graphql-on-various-pg

Hasura's GraphQL engine on various Postgres systems/providers
Shell
8
star
89

cli-plugins-index

8
star
90

ndc-sdk-typescript

NDC SDK for TypeScript
TypeScript
8
star
91

graphql-weather-api

A simple GraphQL express weather api server boilerplate
JavaScript
8
star
92

supergraph-top-n-challenge

JavaScript
8
star
93

haskell-docker-builder

Package haskell binaries as docker images
Makefile
7
star
94

graphql-engine-install-manifests

Various installation manifests for Hasura's GraphQL Engine
Shell
7
star
95

laravel-todo-hge

A sample Laravel app with an auth webhook
PHP
7
star
96

awesome-react-fullstack

A review of the must know concepts & tools for going fullstack with react. Awesome list to the top tools and learning resources for each concept.
7
star
97

weaviate_gdc

POC: Weaviate data connector
TypeScript
7
star
98

ai-workshop-hasuracon23

Jupyter Notebook
6
star
99

ndc-postgres

Hasura v3 Data Connector for PostgreSQL
Rust
6
star
100

trigger-serverless-zeit-example

JavaScript
6
star