I want to make a chart like this in Recharts.
They have a similar chart but the edges of it's lines are straight and not round like I need. Can it be configured?
Yeah we can make that double radial bar chart using recharts. This is react/ next js Component. You can modify Bar Radius, Inner radius. For Inner text there is a single number given from Recharts you can use Positioning for rendering that inner text and stylings.
import React from 'react';
import { RadialBarChart, PolarAngleAxis, RadialBar } from 'recharts';
const index = () => {
// Sample data
const data = [
{ name: 'A', x: 1, fill: 'green' },
{ name: 'B', x: 2, fill: 'yellow' },
// { name: 'C', x: 3, fill: 'aqua' },
// { name: 'D', x: 4, fill: 'blue' },
// { name: 'E', x: 5, fill: 'orange' },
// { name: 'F', x: 6, fill: 'red' },
// { name: 'G', x: 7, fill: 'black' },
// { name: 'H', x: 8, fill: 'purple' },
// { name: 'I', x: 9, fill: 'gray' },
];
return (
<RadialBarChart width={143} height={143} data={data}
// cx={30 / 2}
// cy={30 / 2}
innerRadius={25}
// outerRadius={18}
barSize={4}
startAngle={90}
endAngle={-270}>
<PolarAngleAxis
type="number"
domain={[0, 10]}
angleAxisId={0}
tick={false}
/>
<RadialBar
background
dataKey="value"
cornerRadius={30 / 2}
fill="#0BEFF2"
/>
<text
x={30 / 2}
y={33 / 2}
textAnchor="middle"
dominantBaseline="middle"
className="progress-label"
>
89
</text>
</RadialBarChart>
);
};
export default index;
Related
I am using react-plotly.js Bar-Chart for which I want to show a Tooltip when we hover over a particular Y-axis label that will include a delete button with a data summary.
I have used a custom tooltip first to apply on the label of y-axis but I am not able to see any change on it.
I have also tried a normal HTML tooltip but still it was not visible for the react-plotly.js label.
import { HoverCard, Tooltip } from 'office-ui-fabric-react'
import React, { useState } from 'react'
import Plot from '../Plot'
export default function IndividualView() {
const customTag = '<custom-tag>Hello World</custom-tag>'
const trace1 = {
y: ['Japan', 'United Kingdom', 'Canada', 'Netherlands', 'United States', 'Belgium', 'Sweden', 'Switzerland'],
x: [1.3586, 2.2623000000000002, 4.9821999999999997, 6.5096999999999996,
7.4812000000000003, 7.5133000000000001, 15.2148, 17.520499999999998,
],
name: 'SF Zoo',
type: 'bar',
orientation: 'h',
}
const trace2 = {
y: ['Japan', 'United Kingdom', 'Canada', 'Netherlands', 'United States', 'Belgium', 'Sweden', 'Switzerland'],
x: [1.5, 2.5, 5, 6, 7, 8, 15.5, 18],
name: 'LA Zoo',
type: 'bar',
orientation: 'h',
}
const style = {
width: 950,
height: 500,
// border: '1px solid black',
margin_left: 10,
}
const data = [trace1, trace2]
const layout = {
yaxis: {
categories: ['Japan', 'United Kingdom', 'Canada', 'Netherlands', 'United States', 'Belgium', 'Sweden', 'Switzerland'],
tickInterval: 1,
labels: {
enabled: true,
formatter() {
return `<span title="My custom title">${this.value}</span>`
},
useHTML: true,
style: {
color: 'white',
},
},
},
annotations: [
{
xref: 'paper',
yref: 'paper',
x: -2,
y: -0.109,
showarrow: false,
font: {
family: 'Arial',
size: 1,
color: 'black',
},
},
],
dangerouslySetInnerHTML: { __html: customTag },
}
for (let i = 0; i < data[0].y.length; i++) {
const result = {
xref: 'x',
yref: 'y',
x: 30,
y: data[0].x[i],
text: `${data[0].x[i]}%`,
font: {
family: 'Arial',
size: 12,
color: 'green',
},
showarrow: false,
}
layout.annotations.push(result)
}
const [plotData1] = useState({ data, layout, style })
return (
<Plot {...plotData1} />
)
}
I would like to be able to provide a custom layer on each of my bars in a Nivo responsive bar chart. I have been using this guide as a reference. I am creating a warning level and would like to have a line representing that level set on each bar. The issue I have is that the level is moving around when resizing the page.
The expected response upon screen resizing is for the warning level line to retain its expected position on the chart. What I am getting instead is the warning line moving from its desired position.
"next": "~12.2.4",
"#nivo/bar": "~0.79.1",
"#nivo/core": "~0.78.0",
My chart component using mock data -
import CardComponent from '#components/CardComponent';
import NivoWrapper from '#components/NivoWrapper';
import {
ResponsiveBar,
} from '#nivo/bar';
import React from 'react';
const data = [
{
x: '0',
name: 'weight5',
current: 200,
warningLevel: 100,
},
{
x: '1',
name: 'weight2',
current: 300,
warningLevel: 150,
},
{
x: '2',
name: 'weight3',
current: 400,
warningLevel: 200,
},
{
x: '3',
name: 'weight4',
current: 500,
warningLevel: 250,
},
{
x: '4',
name: 'weight5',
current: 600,
warningLevel: 300,
},
];
const MarginTop = 0;
const Marginright = 24;
const Marginbottom = 24;
const Marginleft = 64;
function Line({ bars, xScale }) {
return (
{bars.map((bar: any) => {
return (
<line
key={bar.key}
y1={bar.absY}
y2={bar.absY + bar.height}
x1={xScale(bar.width - bar.data.data.warningLevel - bar.data.data.warningLevel)}
x2={xScale(bar.width - bar.data.data.warningLevel - bar.data.data.warningLevel)}
stroke="black"
/>
);
})}
);
}
function PackageStockLevelChart() {
return (
<NivoWrapper>
<ResponsiveBar
data={data}
indexBy="name"
keys={['current']}
layout="horizontal"
enableLabel={false}
axisLeft={{
tickValues: 7,
}}
margin={{
top: MarginTop, right: Marginright, bottom: Marginbottom, left: Marginleft,
}}
enableGridY={false}
colors="green"
legendLabel="legend label"
legends={[{
dataFrom: 'keys',
anchor: 'bottom-right',
direction: 'column',
itemWidth: 80,
itemHeight: 16,
translateY: -16,
}]}
layers={['axes', 'bars', Line, 'legends']}
/>
</NivoWrapper>
);
}
export default PackageStockLevelChart;
View before resizing -
View before resizing
During / After resizing
View during after reszing
I am building a React JS application. In my application, I need to display data in Bar chart. I am using this library, https://www.npmjs.com/package/react-chartjs-2. Now, I have a chart like this.
This is the code for that
const colorCode = '#1478BD';
const state = {
data: {
labels: ['J', 'F', 'M', 'A', 'M', 'J', 'J', 'A', 'S', 'O', 'N', 'D'],
datasets: [
{
fill: true,
label: null,
backgroundColor: colorCode,
borderColor: colorCode,
borderWidth: 2,
data: [65, 59, 80, 81, 56, 65, 59, 80, 81, 56, 40, 56]
}
]
},
options: {
plugins: {
legend: {
display: false
},
},
scales: {
x: {
grid: {
display: false
},
beginAtZero: false,
ticks: {
color: colorCode,
}
},
y: {
grid: {
display: false,
},
beginAtZero: true,
ticks: {
color: colorCode,
}
}
},
}
}
<Bar
type={"bar"}
height={170}
data={state.data}
options={state.options}
/>
What I want to do is that I also want to add some background so that I can customing the styling for the remaining gap for each bar. Something like this.
Ass you can see, we can see the background when the bar is not filling the entire height. How can I do that? Is there an option for that?
You can use the beforeDraw plugin for this
const plugins = [
{
beforeDraw: function (chart) {
if (chart.chartArea) {
var ctx = chart.ctx;
var chartArea = chart.chartArea;
var barArray = chart.getDatasetMeta(0).data;
ctx.fillStyle = "#EEE";
for (let i = 0; i < barArray.length; i++) {
const { x, width } = barArray[i];
ctx.fillRect(
x - width / 2,
chartArea.top,
width,
chartArea.bottom - chartArea.top
);
}
}
}
}
];
<Bar
type={"bar"}
height={170}
data={state.data}
options={state.options}
plugins={plugins}
/>
So currently I have this as my code for the victory line chat
import React from 'react';
import { View, StyleSheet } from 'react-native';
import { VictoryLine, VictoryChart } from 'victory-native'
let data = [
{
x: 1,
y: 0
},
{
x: 2,
y: 0
},
{
x: 3,
y: 0
},
{
x: 4,
y: 70
},
{
x: 5,
y: 73
},
{
x: 5,
y: 73
},
{
x: 5,
y: 73
},
{
x: 5,
y: 73
}
]
export default function Graph() {
return (
<VictoryLine style={styles.graph} height={150} width={350} data={data} style={{data: {stroke: 'orange', strokeWidth: 2.5}}} />
)
}
const styles = StyleSheet.create({
graph: {
marginRight: 0
}
})
which gives me a line chart that looks like this. Is there a way to:
A) Plot dotted point on the line for each data point
B) Just dot the last data point in the data list. Example image here of what I want to achieve
You can wrap your VictoryLine with an VictroyChart and hide the axis, like this sample
<View>
<VictoryChart polar={this.state.polar} height={390}>
<VictoryAxis style={{
axis: {stroke: "transparent"},
ticks: {stroke: "transparent"},
tickLabels: { fill:"transparent"}
}} />
<VictoryLine
interpolation={this.state.interpolation} data={data}
style={{ data: { stroke: "#c43a31" } }}
/>
<VictoryScatter data={[data[data.length-1]]}
size={5}
style={{ data: { fill: "#c43a31" } }}
/>
</VictoryChart>
</View>
I'm trying to create a React component that is a Line and Scatter chart to look like this:
The React component for a single line with circles looks like this:
function Line ({ color, chartData }) {
return (
<VictoryGroup data={chartData}>
<VictoryLine
style={{ data: { stroke: color } }}
/>
<VictoryScatter
style={{
data: {
stroke: color,
fill: 'white',
strokeWidth: 3
}
}}
/>
</VictoryGroup>
);
}
I am trying to consume the component like so:
function MyCoolChart () {
return (
<VictoryChart>
<Line
color='#349CEE'
chartData={data1}
/>
<Line
color='#715EBD'
chartData={data2}
/>
</VictoryChart>
);
}
But the Line components aren't rendered. They're only rendered if I call them directly as a function, like so:
export default function MyCoolChart () {
return (
<VictoryChart>
{Line({ color: '#349CEE', chartData: data1 })}
{Line({ color: '#715EBD', chartData: data2 })}
</VictoryChart>
);
}
I'm trying to make a reusable component, so I'd rather not have to consume it as a function. I also want to understand why this is happening. Thanks!
For reference, the values of data1 and data2:
const data1 = [
{ x: 'M', y: 2 },
{ x: 'Tu', y: 3 },
{ x: 'W', y: 5 },
{ x: 'Th', y: 0 },
{ x: 'F', y: 7 }
];
const data2 = [
{ x: 'M', y: 1 },
{ x: 'Tu', y: 5 },
{ x: 'W', y: 5 },
{ x: 'Th', y: 7 },
{ x: 'F', y: 6 }
];
Thanks to #boygirl for responding to my github issue
Turns out Victory passes a few props down of its own that need to be passed along for things to be rendered correctly. Examples of this are domain and scale. Here's my updated component:
function Line ({ color, ...other }) {
return (
<VictoryGroup {...other}>
<VictoryLine
style={{ data: { stroke: color } }}
/>
<VictoryScatter
style={{
data: {
stroke: color,
fill: 'white',
strokeWidth: 3
}
}}
/>
</VictoryGroup>
);
}
And it is now consumed like so:
function MyCoolChart () {
return (
<VictoryChart>
<Line
color='#349CEE'
data={data1}
/>
<Line
color='#715EBD'
data={data2}
/>
</VictoryChart>
);
}