I'm Having a hell of a time trying to get custom labels on a recharts radar chart working. I've tried doing multiple things according to the docs so I figured I'd throw it to the wolves. Hopefully one of you can point me in the right direction.
<ResponsiveContainer>
<RadarChart outerRadius={125} cy={175} data={ucRadarData}>
<Legend verticalAlign='top' height={36} />
<PolarGrid />
<PolarAngleAxis dataKey='value'>
<Label content={({ value }) => <Typography>{value}</Typography>} /> // When I remove this component, the labels get removed as well so I assume this is the component I want to target.
</PolarAngleAxis>
<PolarRadiusAxis domain={[lowestRangeStart, highestRangeEnd]} tickCount={tickArray.length} />
<Radar label={({ label }) => <Typography>{label}</Typography>} name='Self Assessment' dataKey='Self' stroke='#8884d8' fill='#8884d8' fillOpacity={0.6} /> // Also tried adding custom label here.
</RadarChart>
</ResponsiveContainer>
Do this:
Make a render function for custom tick and add that as tick prop to your PolarAngleAxis component
function customTick({ payload, x, y, textAnchor, stroke, radius }) {
return (
<g
className="recharts-layer recharts-polar-angle-axis-tick"
>
<text
radius={radius}
stroke={stroke}
x={x}
y={y}
className="recharts-text recharts-polar-angle-axis-tick-value"
text-anchor={textAnchor}
>
<tspan x={x} dy="0em">
{payload.value}
</tspan>
</text>
</g>
);
}
<PolarAngleAxis dataKey='value' tick={customTick}/>
Related
I'd like to put more than one line in the x-axis ticks in a bar chart, something like this:
But all the props I have to control what renders there, underneath each group of bars, is the dataKey, so all I can get with Recharts is:
This is just rendering one value from my data.
Ideally I'd like to be able to pass XAxis a prop to render this such as:
<XAxis dataKey="name" render={(value, dataPoint) => (
<CustomXAxisName>
<p>{value}</p>
<p>{dataPoint.total} Principals</p>
</CustomXAxisName>
)} />
but I can find no affordances for this. There is a more flexible prop to configure label, but that refers to one label for the whole axis.
Is there anyway to get this? I'm quite surprised that I can't find a natural way to do this, since Rechart since so flexible in other regards and I don't think this is such a weird idea.
Have you looked at https://recharts.org/en-US/examples/CustomizedLabelLineChart?
It shows how to put something diagonally in the x-axis, surely it can be used to put multiple lines.
It uses the tick={<CustomTick/>} prop to the XAxis component.
You can do it like:
const customizedGroupTick = (props: any) => {
const { index, x, y, payload } = props;
return (
<g>
<g>
<text x={x} y={y}>
data
</text>
<text x={x} y={y }>
data
</text>
</g>
</g>
);
};
and then
<XAxis
dataKey="Date"
axisLine={false}
tickLine={false}
scale="band"
tick={customizedGroupTick}
interval={0}
/>
I am trying to make a custom label by passing a React Component to the content props of Label component and the label doesn't show.
My goal is to display the label at the center of a pie chart. Below is my code snippet and here is a working sample.
<PieChart>
<Pie
isAnimationActive={false}
data={pieArray}
innerRadius={100}
outerRadius={150}
dataKey="value"
cy="50%"
cx="50%"
startAngle={90}
endAngle={-270}
>
{pieArray.map(({ data }, id) => (
<Cell key={data} fill={chartColors[id % 8]} />
))}
<Label
content={<CustomLabel />}
position="center"
/>
</Pie>
</PieChart>
Pie Chart
function CustomLabel(props: any) {
return (
<div>
<h1 className="custom">Custom Label</h1>
<p>Testing</p>
</div>
);
}
Custom Label
I am using React version 17.0.2 and recharts 2.1.2. Link to code sample. Thanks.
I am using Recharts BarChart components to visualize my data. In the Bar component, it supports a few events handler (onClick, onMouseDown, etc).
My question: I have an onClick event handler so users are able to click on the bar chart and redirect to other pages based on the result ID.
My problem: If the bar chart's value is at 0, users are unable to click on the link since onClick event handler is implemented on the bar itself.
Source code:
<BarChart
width={500}
height={500}
layout="vertical"
data={data}
>
<XAxis type="number" />
<YAxis dataKey="title" type="category" />
<Bar
dataKey="score"
fill={Color}
label={<CustomLabel />}
onClick={(data, index) => {
// some logic here
return history.push(`/${data.x.y.id}`);
}}
/>
</BarChart>
Screenshot:
My attempt:
I tried to look at a possible solution and observe the examples on the API in recharts website, I don't see anything that is able to solve my problem. I also tried to have onClick on the title on axis or bar, but the component does not support the feature. Please advise.
You can add an onClick attribute on the BarChart component and check data.activePayload[0] to see which bar chart has been clicked.
<BarChart
width={500}
height={500}
layout="vertical"
data={data}
onClick={(data) => {
// do your history.push based on data.activePayload[0]
if (data && data.activePayload && data.activePayload.length > 0) {
return history.push(`/${data.activePayload[0].x.y.id}`);
}
}}
>
<XAxis type="number" />
<YAxis dataKey="title" type="category" />
<Bar
dataKey="score"
fill={Color}
label={<CustomLabel />}
/>
</BarChart>
I'm working on a react-vis chart that will dynamically update the data that is displayed when a user clicks on/off the legend item for that series.
If a user clicks clicks off all of the series the JSX that I render essentially looks like this:
<div>
<XYPlot
xDomain={paretoOrder}
margin={{left: 150, bottom: 150}}
width={450}
height={450}
xType="ordinal"
stackBy="y"
>
<VerticalGridLines />
<HorizontalGridLines />
<XAxis
tickLabelAngle={-45}
/>
<YAxis
tickFormat={ tick => translator.formatTime(tick, {hideZero: true})}
/>
<VerticalBarSeries
data={[]}
/>
</XYPlot>
<DiscreteColorLegend
orientation="horizontal"
width={300}
items={legendItems}
onItemClick={this.onLegendClick}
/>
</div>
I would expect that this would still render the axes, the grid lines, etc, but the entire chart is removed from the DOM instead. How do I render a chart with empty data but keep the axes, grid lines, etc in the DOM?
Add dontCheckIfEmpty prop (as true, default is false) to <XYPlot> tag.
<XYPlot
dontCheckIfEmpty
>
<XAxis/>
<YAxis/>
<VerticalBarSeries
data={data}
/>
<VerticalGridLines />
<HorizontalGridLines />
</XYPlot>
I am using react slider with tooltip.
<Slider
min={ set_min(this.state.myValue) }
max={ set_max(this.state.myValue) }
defaultValue={ set_def(this.state.myValue) }
handle={handle}
toolTipVisibleAlways = {true}
/>
<Tooltip
className = "tooltip-custom"
prefixCls="rc-slider-tooltip"
overlay={percentFormatter(value, this.state.myValue) }
visible={dragging}
placement="top"
key={index}
delayShow = {300}
delayHide = {150}
>
<Handle value={value} {...restProps} />
</Tooltip>
all things are good and well-displayed.
what i want to know is that how can i make tooltip always visible.
I searched this answer on the internet and found two answers.
first one is toolTipVisibleAlways = {true}
and second one is
delayShow = {300}
delayHide = {150}
But as you can see my code, nothing changed.
Use tipProps props exposed after Slider wrapped by createSliderWithTooltip. Example below should work as tipProps will pass the props to rc-tooltip component.
Read here for detailed explanation and also see this example provided by rc-slider
.
<Slider
min={ set_min(this.state.myValue) }
max={ set_max(this.state.myValue) }
defaultValue={ set_def(this.state.myValue) }
handle={handle}
tipProps={{visible:true}}
/>
I solved this issue by making my own inside the handle. This way the text will always move with the handle.
<SliderTooltip
prefixCls="rc-slider-tooltip"
visible={dragging}
placement="top"
key={index}
defaultVisible={true}
visible={true}
align={{
offset: [0, -5],
}}
>
<Handle value={value} {...restProps}>
<span>{value}</span>
</Handle>
</SliderTooltip>