c3js / c3

:bar_chart: A D3-based reusable chart library
http://c3js.org
MIT License
9.34k stars 1.39k forks source link

Add normalized stack support #2670

Closed panthony closed 5 years ago

panthony commented 5 years ago

Can be configured using:

{
  data: {
    stack: {
      normalize: true
    }
  }
}

Defaults to false.

Closes #142

panthony commented 5 years ago

@kt3k Hello there, I was looking to cache some function returns and I noticed the cache.js file.

Look how it is used I found that this is mostly used by load() through cacheIds but:

  1. There are no documentation whatsoever nor issues about this
  2. Weird to have a public API passing "cache ids"

Do you know how/if this is used ? Would it be possible to drop the support for it ?

My intent would be to modify the cache interface to be used in more contexts internally.

codecov-io commented 5 years ago

Codecov Report

Merging #2670 into master will decrease coverage by 0.04%. The diff coverage is 80.37%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2670      +/-   ##
==========================================
- Coverage   80.13%   80.08%   -0.05%     
==========================================
  Files          57       57              
  Lines        4520     4605      +85     
==========================================
+ Hits         3622     3688      +66     
- Misses        898      917      +19
Impacted Files Coverage Δ
src/config.js 95.83% <ø> (ø) :arrow_up:
src/data.load.js 58% <100%> (+1.75%) :arrow_up:
src/domain.js 93.75% <100%> (+0.07%) :arrow_up:
src/shape.bar.js 98.66% <100%> (+0.01%) :arrow_up:
src/cache.js 63.15% <100%> (+26.79%) :arrow_up:
src/tooltip.js 77.23% <100%> (+0.37%) :arrow_up:
src/api.data.js 100% <100%> (ø) :arrow_up:
src/axis.js 97.56% <100%> (ø) :arrow_up:
src/arc.js 73.33% <50%> (-0.63%) :arrow_down:
src/shape.js 76.92% <69.23%> (-1.77%) :arrow_down:
... and 2 more

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update a4c971c...c54d5cf. Read the comment docs.

codecov-io commented 5 years ago

Codecov Report

Merging #2670 into master will increase coverage by 0.29%. The diff coverage is 94.4%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2670      +/-   ##
==========================================
+ Coverage   80.29%   80.58%   +0.29%     
==========================================
  Files          59       59              
  Lines        4537     4631      +94     
==========================================
+ Hits         3643     3732      +89     
- Misses        894      899       +5
Impacted Files Coverage Δ
src/config.js 95.83% <ø> (ø) :arrow_up:
src/scale.js 100% <100%> (ø) :arrow_up:
src/shape.bar.js 98.66% <100%> (+0.01%) :arrow_up:
src/data.load.js 58% <100%> (+1.75%) :arrow_up:
src/cache.js 63.15% <100%> (+26.79%) :arrow_up:
src/domain.js 93.75% <100%> (+0.07%) :arrow_up:
src/util.js 100% <100%> (ø) :arrow_up:
src/axis.js 97.58% <100%> (+0.02%) :arrow_up:
src/tooltip.js 77.23% <100%> (+0.37%) :arrow_up:
src/arc.js 74.57% <50%> (-0.6%) :arrow_down:
... and 3 more

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update bdf12d1...9b94955. Read the comment docs.

panthony commented 5 years ago

Big part of untested added code is to be used when padAngle option will be available (see #2669).

Can't add a test for it right now :/

kt3k commented 5 years ago

@panthony Personally I think these cache APIs don't belong to "public API" and we can change (or replace) them as long as the existing charts work.

panthony commented 5 years ago

@kt3k One of the usage of this cache was "recently" added for gauge, but I think the value could be retrieved another way:

return $$.levelColor($$.cache[id].values[0].value);

Otherwise, the only usage is load({ cacheIds: [] }). But I don't really know how it was (is?) used.

kt3k commented 5 years ago

The example looks great! 👍

スクリーンショット 2019-08-10 19 38 32
panthony commented 5 years ago

@kt3k I added a new exemple of a combinaison chart using stack normalisation per axis basis:

Capture d’écran 2019-08-13 à 15 38 50

shaswa commented 4 years ago

Hi! Thank you so much for this feature! I'm trying to use normalized stacked bar chart in a project. It works as expected except for the tooltip. The tooltip still shows the original values, instead of normalized or percentage values. Do I need to set anything else to display the tooltip values in percentages?

Edit: Figured out a solution almost as soon as I posted this 😅

tooltip: {
  format: {
    value(value, ratio) {
      return `${ratio * 100}%`;
    },
  },
}