sc0ttj / component

A tiny library for isomorphic JavaScript components
MIT License
2 stars 1 forks source link

[WIP] Feature/charts #55

Closed sc0ttj closed 2 years ago

sc0ttj commented 2 years ago

Charts

A chart/graph making add-on.

Only 3.5kb minified & gzipped so far, also works standalone (Component not required).

TO DO

Addons:

Usage:

      ctx.beginPath()

      .useData({
        'USA': [{}, // pad with an empty year.. just to create more space on chart
          { medals: 25 },
          { medals: 15 },
          { medals: 21 },
          { medals: 42 },
          { medals: 47 },
          { medals: 21 },
          { medals: 24 },
        ],
        'China': [{},
          { medals: 10 },
          { medals: 10 },
          { medals: 25 },
          { medals: 40 },
          { medals: 32 },
          { medals: 12 },
          { medals: 21 },
        ]
      })

      // top, bottom, left, right
      .margin(50,60,120,40)  

      .xAxis({
        range: [1999,2006],
        scale: 1,
        // optional settings
        label: "Years",
        labelBelow: true,
        yPos: 0,
        tickLength: -3,
        tickCentered: true,
        tickLabelCentered: true,
        tickLabels: [], // can be func which receives each label, like `label => {...}`
      })

      .yAxis({
        range: [0,16],
        scale: 1,
        // optional settings
        label: "Gold medals",
        labelLeft: true,
        xPos: 0,
        tickLength: -3,
        tickCentered: false,
        tickLabels: [], // can be func which receives each label, like `label => {...}`
      })

      .drawEach((country, key, i) => {
        // Now we can work with some "decorated" chart data, ${country}.
        // Use your top-level data here - to draw a legend, for example..

        country.forEach(data => {
          // Note, ${data} has .bar(), .circle(), and .line() methods added,
          // which try to draw "ready-made" shapes, in the right place.

          // You should only define the attrs you want "joined" to your
          // data - and don't override any others.
        data.bar({
          height: data.medals,
          stacked: false,
          padding: 24,
          style:{ fill: colors[i] },
        });
        data.circle({
          cy:     data.medals,
          radius: data.medals*2,
          start: 0,
          end: 360,
          rotate: 0,
          style: { fill: colors[i+2], lineWidth: 5, stroke: 'red' },
        });
        data.pie({
          slice: data.medals,
          style: { fill: colors[n] },
        });
        data.arc({
          slice: data.medals,
          radius: 80,
          innerRadius: 40,
          style: { fill: colors[n] },
        });
        data.line({
          py: data.medals,
          stacked: true,
          style: {
            fill: colors[i],
            stroke: colors[i],
            lineWidth: 0,
          },
        });

    });

Outputs:

bar

..easily flip swap the axes by changing a few lines of code:

hbar

..same chart, different settings:

line

Newer demo:

charts