Code Monkey home page Code Monkey logo

jscharting-vue's Introduction



JSCharting for Vue.js
JavaScript data visualization for Vue.js

Builds David npm version code style Twitter


JSCharting is a JavaScript data visualization library offering seamless usage with Vue across all devices and platforms. Every JSCharting license includes a full suite of 150+ chart types including standards such as pie charts, line charts, donut and bar charts. In addition, advanced chart types including Gantt charts, JavaScript Org Charts, interactive charts for stock and finance, seamless grid and calendar charts, JavaScript maps, sparklines, and micro charts all for no additional charge. JSCharting has all the features you need and many you don't yet know you want.

Example Charts: Chart Types | Feature Examples

Official JSCharting plugin for Vue.js

A Vue2 and Vue3 wrapper to use JSCharting charting library as a Vue component.

This documentation is for Vue 2.x and earlier. For Vue3, click here.

Table of Contents

  1. Install
    1. Run Examples
  2. Usage
    1. Simple Example
    2. JSCharting Component Options
    3. Typescript Typings
    4. Updating Charts
  3. Chart resources
  4. Getting a chart reference
  5. JSCLabel Component
  6. JSCGrid Component

Install

Install the jscharting-vue chart component.

npm i -D jscharting-vue

or

yarn add jscharting-vue

Run Examples

Clone the Github repo locally. Example charts are located in the /examples folder.

To view the examples you can run the webpack dev server: localhost:8080

npm run serve-examples

Or build the project manually.

npm run build-examples

Usage

Simple example

This example shows how you can use the JSCharting component of the jscharting-vue module to make a column chart.

<template>
    <JSCharting :options="chartOptions" class="columnChart"></JSCharting>
</template>

<script>
import JSCharting from 'jscharting-vue';

export default {
   name: 'columnChart',
   data() {
      return {
         chartOptions: {
            type: 'horizontal column',
            series: [
               {
                  points: [
                     { x: 'A', y: 50 },
                     { x: 'B', y: 30 },
                     { x: 'C', y: 50 }
                  ]
               }
            ]
         }
      }
   },
   components: {
      JSCharting
   }
}
</script>

<style>
.columnChart {
    height: 300px;
}
</style>

JSCharting Component Options

These customizable options are available with the JSCharting component.

Parameter Type Description
:options object JSCharting chart configuration object. Please refer to the API documentation.
:mutable boolean (Optional) When set to true, chart.options() is called with the updated props instead of recreating the chart object.
:ignoreStateUpdate boolean (Optional) false by default. When true, the chart will ignore updates applied when reactive property is changed. This is useful when you want to update the chart directly.
@rendered event (Optional) Event triggered when the chart is finished rendering. The first argument of the event handler is a reference to the created chart.

Typescript Typings

You can use type definitions for chart configuration properties when using typescript. Import the types using:

import { JSCChartConfig } from 'jscharting'; // -> JSCChartConfig
// OR
import JSCharting, { JSC } from 'jscharting-vue'; // -> JSC.JSCChartConfig

Here's an example using types.

<template>
  <div>
    <JSCharting :options="options"></JSCharting>
  </div>
</template>

<script lang="ts">
import JSCharting, {JSC} from 'jscharting-vue';


export default {
   name: 'exampleChart',
   data() {
      let options: JSC.JSCChartConfig = {
         type: 'line', 
         title: { label: { text: 'Chart Tytle' } },
         legend: {
            position: 'inside bottom right'
         },
         xAxis: { scale: { type: 'time' } },
         series: [
            {
               name: 'Purchases',
               points: [
                  ['1/1/2020', 29.9],
                  ['1/2/2020', 71.5],
                  ['1/3/2020', 106.4],
                  ['1/6/2020', 129.2],
                  ['1/7/2020', 144.0],
                  ['1/8/2020', 176.0]
		       ]
            }
         ]
      };
      return { options };
  },
  components: {
     JSCharting
  }
};
</script>

Updating charts

There are a couple ways to update live charts.

Using reactive properties

Updating reactive data properties affects the chart in two ways, depending on the option mutable. When the component option mutable is true, only new options are passed to the chart using chart.options(). When mutable is false, changing data will reset the chart with a new instance.

Charts with mutable == true option perform better and allow charts to animate changes. Only new options that are changing need to be passed to the chart. You can animate chart updates using this more.

Using mutable == false is sometimes useful when a chart must be drastically modified. In this mode, all options should be available for a new chart instance to use.

See animating series and points for more information.

<template>
    <div>
        <JSCharting :options="options"></JSCharting>
        <button v-on:click="updateData">Update Data</button>
    </div>
</template>

<script>
import JSCharting from 'jscharting-vue';

export default {
   name: 'methodUpdate',
   data() {
      return {
         options: {
            series: [
               {
                  name: 'Purchases',
                  points: this.randomPoints()
               }
            ]
         }
      };
   },
   methods:{
      updateData() {
         this.options = {
            series: [
               {
                  name: 'Purchases',
                  points: this.randomPoints()
               }
            ]
         };
      },
      randomPoints: () => {
         let points = [];
         for (let i = 1; i < 12; i++) {
            points.push({ id: 'p' + i, x: `1/${i * 2}/2020`, y: Math.random() * 10 });
         }
         return points;
      }
   },
   components: {
      JSCharting
   }
};
</script>
Updating chart directly

JSCharting has a rich API for direct interaction with chart elements programmatically. This approach is more flexible and can update the chart more efficiently when performance is a priority. Charts can also be decoupled from data updates and managed independently.

Set the ignoreStateUpdate option to true when you want to use reactive properties for other purposes but not affect the chart itself.

See getting a chart reference. Once a chart reference is available, you can update chart options as needed with code such as:

this.$refs.chart.instance.series().points(p => p.y > 50).options({ color: "red" });

This line will make all points on a chart with y values greater than 50 red. Another example:

this.$refs.chart.instance.series(0).points(0).options({ y: 100 });

This updates the "Purchases" series with new random points.

<template>
    <div>
        <JSCharting :options="options" ref="chart"></JSCharting>
        <button v-on:click="updateData">Update Data</button>
    </div>
</template>

<script>
import JSCharting from 'jscharting-vue';

export default {
   name: 'methodUpdate',
   data() {
      return {
         options: {
            series: [
               {
                  name: 'Purchases',
                  points: this.randomPoints()
               }
            ]
         }
      };
   },
   methods:{
      updateData() {
         const chart = this.$refs.chart.instance;
         if(chart){
            chart.series('Purchases').options({points: this.randomPoints()})
         }
      },
      randomPoints: () => {
         let points = [];
         for (let i = 1; i < 12; i++) {
            points.push({ id: 'p' + i, x: `1/${i * 2}/2020`, y: Math.random() * 10 });
         }
         return points;
      }
   },
   components: {
      JSCharting
   }
};
</script>

Chart resources

The JSCharting library includes resources (modules, mapping data, polyfills, icons library) that load automatically when they are needed. The examples/ webpack vue.config.js configuration copies these resources to the ./dist/assets/jscharting folder during built.

The examples/src/App.vue file calls the JSC.defaults() function to set baseUrl option with this path globally in its constructor. All subsequent charts will be aware of the location of these resources.

import { JSC } from 'jscharting-vue';
JSC.defaults({ baseUrl: 'dist/assets/jscharting', debug:true });

Note: If the chart does not find the resources path, it will download them from a CDN. Setting debug:true in the JSC.defaults() function during development is recommended as it will alert you when the CDN fallback is used. It is recommended to use a local copy of resources in production.

Getting a chart reference

You can get a chart instance using the ref attribute:

<template>
    <JSCharting :options="options" ref="chart"></JSCharting>
</template>

<script>
import JSCharting from 'jscharting-vue';

export default {
   methods: {
      getChart() {
         return this.$refs.chart.instance;
      }
   },
   components: {
      JSCharting
   }
};
</script>

You can also store it when the chart @rendered event is executed.

<template>
    <JSCharting :options="options" @rendered="callback"></JSCharting>
</template>

<script>
import JSCharting from 'jscharting-vue';

export default {
   mounted() {
      // Using the chart reference.
         this.chart && 
            this.chart.series.add({ name: "S1", points: [{ x: 5, y: 10 }, { x: 5, y: 10 }] });
   },
   data() {
      return {
         chart: null
      }
   },
   methods:{
      callback(chart) {
         this.chart = chart;
      }
   },
   components: {
      JSCharting
   }
};
</script>

JSCLabel Component

This plugin also contains an implementation of the JSCLabel component for vue. You can use it to create very efficient micro chart SVG images in your vue projects. Here's a simple example.

<template>
    <JSCLabel :options="options" />
</template>

<script>
   import { JSCLabel } from 'jscharting-vue';

   export default {
      name: 'Microchart',
      data() {
         const data = [5,2,3,5,1];
            return {
               options: `<chart arealine data=${data.join(',')} width=200 height=50>`
            };
      },
      components: {
         JSCLabel
      }
   };
</script>

The Microchart Fast example demonstrates updating a microchart using the JSCLabel component quickly.

See the microcharts tutorial for configuration string syntax and more information.

JSCGrid Component

The JSCGrid data grid component is also included. You can use it to create data grids from JSON arrays. Here's a data grid example.

<template>
    <JSCGrid :options="options" />
</template>

<script>
   import { JSCGrid } from 'jscharting-vue';
   export default {
      name: 'Data Grid',
      data() {
         return {
            options: {
               data: [
                  ['Art', 5, 10],
                  ['Greg', 3, 6],
                  ['Olivia', 11, 8],
                  ['Steve', 11, 4],
                  ['Anna', 3, 8]
               ],
               columns: [
                  { header: 'Name' },
                  { header: 'Value One' },
                  { header: 'Value Two' },
                  { header: 'Sum', value: '{%1+%2}' }
               ]
            }
         };
      },
      components: {
         JSCGrid
      }
   };
</script>

The available options for the data grid component are.

Parameter Type Description
:options object JSCGrid configuration object. Please refer to the API documentation.
:mutable boolean (Optional) When set to true, grid.options() is called with the updated props instead of recreating the grid instance.
@rendered event Triggered when the grid is finished rendering. The first argument of the event handler is a reference to the created grid.
import { JSCGridConfig } from 'jscharting'; // -> JSCGridConfig
// OR
import JSCharting, { JSC } from 'jscharting-vue'; // -> JSC.JSCGridConfig

See the data grid tutorial for configuration syntax and more information.

jscharting-vue's People

Contributors

arthurpuszynski avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

jscharting-vue's Issues

Error with Vue3

I copied and pasted the example in the vue3 documentation and I keep getting:
Uncaught SyntaxError: The requested module '/node_modules/jscharting/dist/jscharting.js?v=c78523e5' does not provide an export named 'Chart' (at jscharting.vue:6:10)

<template>
    <JSCharting :options="chartOptions" ></JSCharting>
</template>

<script lang="ts">
import { defineComponent, reactive } from 'vue'
import JSCharting, { JSC } from 'jscharting-vue';

export default defineComponent({
   name: 'columnChart',
   setup() {
      const chartOptions = reactive({
         type: 'horizontal column',
         series: [
            {
               points: [
                  { x: 'A', y: 50 },
                  { x: 'B', y: 30 },
                  { x: 'C', y: 50 }
                  ]
              }
          ]
        } as JSC.JSCChartConfig);
      return { chartOptions }; 
    },
    components: {
        JSCharting
    }
});
</script>

Quasar support

I have tested Apexcharts and Highcharts, and I am hoping to get JSCharting working. I get the following error with normal code, but even as simple as this. It should be failing because it can't find options.

Uncaught SyntaxError: ambiguous indirect export: Chart jscharting.vue:2:9

<template>
  <JSCharting
    ref="chartZilla"
    class="columnChart"
    :options="options"
  />
</template>

<script setup>
import JSCharting from 'jscharting-vue';
</script>

I am hoping to use the export server to create static images.

Error loading resources locally: Unexpected token '<'

I used npm to install jscharting-vue and it was working perfectly. Then I edited my webpack config so that the resouces would be copied to a local folder to avoid using a CDN, as per the documentation.

However, now when I load a page with charts I get this error:
Uncaught SyntaxError: Unexpected token '<'
at pw (jscharting.js:84:1)
at success (jscharting.js:83:1)
at b.onreadystatechange (jscharting.js:85:1)

I have not changed anything other than using webpack to copy the resources to a local folder (I have confirmed they are indeed copied), and added the baseUrl to my main.js.

If anyone has any hints or ideas, please do let me know.

Chart as grid doesn't work with multiple charts in jscharting-vue but does when just using plain javascript

Issue
When using more than one chart on the same page, setting
grid: { enabled: true }
Only one of the chart displays a grid correctly when clicking on the "Grid" button.
image
image
When creating the exact same charts using pure js and html it works correctly.
image
image

Expected Behaviour
The grid behaviour should work for all charts on the page in vue, in the same way that it does for pure js.

Observed Behaviour
Only one of the grids works correctly. Only one of the charts works correctly, regardless of how many charts there are (I've gone up to 4).

Pure js

<!DOCTYPE html><html lang="en">
  <head>
    <title>Basic Donut Chart | JSCharting</title>
    <meta http-equiv="content-type" content="text-html; charset=utf-8">
    <script src="https://code.jscharting.com/latest/jscharting.js"></script>
    <script type="text/javascript" src="https://code.jscharting.com/latest/modules/types.js"></script>
    <style type="text/css"></style>
  </head>
  <body>
    <div id="chartDiv" style="max-width: 450px;height: 360px;margin: 0px auto"></div>
    <div id="columnDiv" style="max-width: 740px;height: 400px;margin: 0px auto">
    </div>
  <script type="text/javascript">
    var chart = JSC.chart('chartDiv', {
      debug: true,
      grid: {
        enabled: true,
      },
      legend: {
        position: 'inside left bottom',
        template: '%value {%percentOfTotal:n1}% %icon %name'
      },
      title_position: 'center',
      defaultSeries_type: 'pieDonut',
      defaultPoint_label_text: '<b>%name</b>',
      title_label_text: 'Countries GDP',
      yAxis: { label_text: 'GDP', formatString: 'n'  },
      series: [
        {
          name: 'Countries',
          points: [
            { name: 'United States', y: 5452500  },
            { name: 'Canada', y: 786052  },
            { name: 'United Kingdom', y: 477338  },
            { name: 'Mexico', y: 155313  }
          ]
        }
      ]
    });
    var secondChart = JSC.chart('columnDiv', { 
      debug: true, 
      grid: {
        enabled: true,
      },
      type: 'column', 
      yAxis: { 
        scale_type: 'stacked', 
        label_text: 'Units Sold'
      }, 
      title_label_text: 'Acme Tool Sales', 
      xAxis_label_text: 'Quarter', 
      series: [ 
        { 
          name: 'Saw', 
          id: 's1', 
          points: [ 
            { x: 'Q1', y: 230 }, 
            { x: 'Q2', y: 240 }, 
            { x: 'Q3', y: 267 }, 
            { x: 'Q4', y: 238 } 
          ] 
        }, 
        { 
          name: 'Hammer', 
          points: [ 
            { x: 'Q1', y: 325 }, 
            { x: 'Q2', y: 367 }, 
            { x: 'Q3', y: 382 }, 
            { x: 'Q4', y: 371 } 
          ] 
        }, 
        { 
          name: 'Grinder', 
          points: [ 
            { x: 'Q1', y: 285 }, 
            { x: 'Q2', y: 292 }, 
            { x: 'Q3', y: 267 }, 
            { x: 'Q4', y: 218 } 
          ] 
        }, 
        { 
          name: 'Drill', 
          points: [ 
            { x: 'Q1', y: 185 }, 
            { x: 'Q2', y: 192 }, 
            { x: 'Q3', y: 198 }, 
            { x: 'Q4', y: 248 } 
          ] 
        } 
      ] 
    }); 
  </script>
</body>
</html>

Vue code
Can be found here: https://github.com/ltaylor2607/chart-test

This is a really fantastic feature and it would be great to be able to make use of it in Vue, so any help with this would be greatly appreciated.

Cannot read property min of undefined

I am trying to integrate this graph with my data on a laravel backend. I am pulling the data in correct format from the demos / examples.

series: [
    {
        name: 'Series 1',
        points: [{x: <timestamp>, y: <number>}, {x: <timestamp>, y: <number>}, {x: <timestamp>, y: <number>}]
    },
    {
        name: 'Series 2',
        points: [{x: <timestamp>, y: <number>}, {x: <timestamp>, y: <number>}, {x: <timestamp>, y: <number>}]
    }
]

I have about 10 000 points that I am trying to load. The names pop up in the legend. But I get the error Uncaught TypeError: Cannot read property 'min' of undefined and it doesn't load any graph.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.