Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
chartjs-plugin-stacked100
Advanced tools
This plugin for Chart.js that makes your bar chart to 100% stacked bar chart
This plugin for Chart.js that makes your bar chart to 100% stacked bar chart.
Requires Chart.js v3 or higher.
Demo: https://y-takey.github.io/chartjs-plugin-stacked100
yarn add chartjs-plugin-stacked100
or
npm install chartjs-plugin-stacked100 --save
import { Chart } from "chart.js";
import ChartjsPluginStacked100 from "chartjs-plugin-stacked100";
Chart.register(ChartjsPluginStacked100);
<script src="https://cdn.jsdelivr.net/npm/chart.js@3.0.0/dist/chart.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-stacked100@1.0.0"></script>
Chart.register(ChartjsPluginStacked100.default);
Name | Type | Default | Description |
---|---|---|---|
enable | boolean | false | |
replaceTooltipLabel | boolean | true | replace tooltip label automatically. |
fixNegativeScale | boolean | true | when datasets has negative value and fixNegativeScale is false , the nagative scale is variable. (the range is between -100 and -1 ) |
individual | boolean | false | scale the highest bar to 100%, and the rest would be proportional to the highest bar of a stack. |
round | "off" | "down" | "up" | "off" | Use "off" to Math.round , "down" to Math.floor , and "up" to Math.ceil . |
precision | number | 1 | precision of percentage. the range is between 0 and 16 |
axisId | string | - | This option allows you to stack only the axis in a chart that includes multiple axes. By default, the plugin will attempt to stack all axes |
specify plugin options with { stacked100: { enable: true } }
.
specify plugin options with { stacked100: { enable: true, replaceTooltipLabel: false } }
.
and you can pass tooltip option.
new Chart(document.getElementById("my-chart"), {
type: "bar",
data: {},
options: {
tooltips: {
callbacks: {
label: (tooltipItem) => {
const data = tooltipItem.chart.data;
const datasetIndex = tooltipItem.datasetIndex;
const index = tooltipItem.dataIndex;
const datasetLabel = data.datasets[datasetIndex].label || "";
// You can use two type values.
// `data.originalData` is raw values,
// `data.calculatedData` is percentage values, e.g. 20.5 (The total value is 100.0)
const originalValue = data.originalData[datasetIndex][index];
const rateValue = data.calculatedData[datasetIndex][index];
return `${datasetLabel}: ${rateValue}% (raw ${originalValue})`;
},
},
},
plugins: {
stacked100: { enable: true, replaceTooltipLabel: false },
},
},
});
You can pass precision
option. (default value is 1
)
For example, when you pass { stacked100: { enable: true, precision: 3 } }
, the result is 0.123%
.
new Chart(document.getElementById("my-chart"), {
type: "bar",
data: {
labels: ["Foo", "Bar"],
datasets: [
{ label: "bad", data: [5, 25], backgroundColor: "rgba(244, 143, 177, 0.6)" },
{ label: "better", data: [15, 10], backgroundColor: "rgba(255, 235, 59, 0.6)" },
{ label: "good", data: [10, 8], backgroundColor: "rgba(100, 181, 246, 0.6)" },
],
},
options: {
indexAxis: "y",
plugins: {
stacked100: { enable: true },
},
},
});
...
// line or bar charts
datasets: [
{ data: [{ y: 5 }, { y: 25 }] },
{ data: [{ y: 15 }, { y: 10 }] },
{ data: [{ y: 10 }, { y: 8 }] }
]
// horizontalBar charts
datasets: [
{ data: [{ x: 5 }, { x: 25 }] },
{ data: [{ x: 15 }, { x: 10 }] },
{ data: [{ x: 10 }, { x: 8 }] }
]
...
new Chart(document.getElementById("my-chart"), {
type: "bar",
data: {},
options: {
plugins: {
datalabels: {
formatter: (_value, context) => {
const data = context.chart.data;
const { datasetIndex, dataIndex } = context;
return `${data.calculatedData[datasetIndex][dataIndex]}% (${data.originalData[datasetIndex][dataIndex]})`;
},
},
stacked100: { enable: true },
},
},
});
npx create-react-app demo-react
cd demo-react
npm install react-chartjs-2 chartjs-plugin-stacked100 --save
Then use it:
import { Chart, Bar } from "react-chartjs-2";
import ChartjsPluginStacked100 from "chartjs-plugin-stacked100";
Chart.register(ChartjsPluginStacked100);
const ChartData = (props: any) => {
return (
<>
{
<div>
<Bar
data={{
labels: ["Foo", "Bar"],
datasets: [
{ label: "bad", data: [5, 25], backgroundColor: "rgba(244, 143, 177, 0.6)" },
{ label: "better", data: [15, 10], backgroundColor: "rgba(255, 235, 59, 0.6)" },
{ label: "good", data: [10, 8], backgroundColor: "rgba(100, 181, 246, 0.6)" },
],
}}
options={{
//@ts-ignore
indexAxis: "y",
plugins: {
stacked100: { enable: true },
},
}}
/>
</div>
}
</>
);
};
export default ChartData;
You can find a working example in the demo-react folder
Pull requests and issues are always welcome.
For bugs and feature requests, please create an issue.
git checkout -b my-new-feature
git commit -am 'Add some feature'
git push origin my-new-feature
yarn install
yarn start
yarn test:watch
or yarn test
npm version (major|minor|patch) && npm publish
yarn dev
yarn gh
FAQs
This plugin for Chart.js that makes your bar chart to 100% stacked bar chart
We found that chartjs-plugin-stacked100 demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.