-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMP] charts: add show values options for charts
Task Description This task aims to add the possibility to show the value of the serie on the chart (above the point for scatter/line chart, above the bar for bar/combo chart and inside part for pie chart). Related Task closes #4324 Task: 3953835 Signed-off-by: Adrien Minne (adrm) <[email protected]>
- Loading branch information
Showing
33 changed files
with
685 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
src/components/figures/chart/chartJs/chartjs_show_values_plugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { ChartType, Plugin } from "chart.js"; | ||
import { chartFontColor } from "../../../../helpers/figures/charts/chart_common"; | ||
import { Color } from "../../../../types"; | ||
|
||
interface ChartShowValuesPluginOptions { | ||
showValues: boolean; | ||
background?: Color; | ||
horizontal?: boolean; | ||
callback: (value: number | string) => string; | ||
} | ||
|
||
declare module "chart.js" { | ||
interface PluginOptionsByType<TType extends ChartType> { | ||
chartShowValuesPlugin?: ChartShowValuesPluginOptions; | ||
} | ||
} | ||
|
||
/** This is a chartJS plugin that will draw the values of each data next to the point/bar/pie slice */ | ||
export const chartShowValuesPlugin: Plugin = { | ||
id: "chartShowValuesPlugin", | ||
afterDatasetsDraw(chart: any, args, options: ChartShowValuesPluginOptions) { | ||
if (!options.showValues) { | ||
return; | ||
} | ||
const drawData = chart._metasets?.[0]?.data; | ||
if (!drawData) { | ||
return; | ||
} | ||
const ctx = chart.ctx; | ||
ctx.save(); | ||
|
||
ctx.textAlign = "center"; | ||
ctx.textBaseline = "middle"; | ||
ctx.fillStyle = chartFontColor(options.background); | ||
ctx.strokeStyle = chartFontColor(ctx.fillStyle); | ||
|
||
chart._metasets.forEach(function (dataset) { | ||
switch (dataset.type) { | ||
case "doughnut": | ||
case "pie": { | ||
for (let i = 0; i < dataset._parsed.length; i++) { | ||
const bar = dataset.data[i]; | ||
const { startAngle, endAngle, innerRadius, outerRadius } = bar; | ||
const midAngle = (startAngle + endAngle) / 2; | ||
const midRadius = (innerRadius + outerRadius) / 2; | ||
const x = bar.x + midRadius * Math.cos(midAngle); | ||
const y = bar.y + midRadius * Math.sin(midAngle) + 7; | ||
|
||
ctx.fillStyle = chartFontColor(bar.options.backgroundColor); | ||
ctx.strokeStyle = chartFontColor(ctx.fillStyle); | ||
|
||
const value = options.callback(dataset._parsed[i]); | ||
ctx.strokeText(value, x, y); | ||
ctx.fillText(value, x, y); | ||
} | ||
break; | ||
} | ||
case "bar": | ||
case "line": { | ||
const yOffset = dataset.type === "bar" && !options.horizontal ? 0 : 3; | ||
for (let i = 0; i < dataset._parsed.length; i++) { | ||
const point = dataset.data[i]; | ||
const value = options.horizontal ? dataset._parsed[i].x : dataset._parsed[i].y; | ||
const displayedValue = options.callback(value - 0); | ||
let xPosition = 0, | ||
yPosition = 0; | ||
if (options.horizontal) { | ||
yPosition = point.y; | ||
if (value < 0) { | ||
ctx.textAlign = "right"; | ||
xPosition = point.x - yOffset; | ||
} else { | ||
ctx.textAlign = "left"; | ||
xPosition = point.x + yOffset; | ||
} | ||
} else { | ||
xPosition = point.x; | ||
if (value < 0) { | ||
ctx.textBaseline = "top"; | ||
yPosition = point.y + yOffset; | ||
} else { | ||
ctx.textBaseline = "bottom"; | ||
yPosition = point.y - yOffset; | ||
} | ||
} | ||
ctx.strokeText(displayedValue, xPosition, yPosition); | ||
ctx.fillText(displayedValue, xPosition, yPosition); | ||
} | ||
break; | ||
} | ||
} | ||
}); | ||
|
||
ctx.restore(); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.