T M
T M

Reputation: 530

Victory-native's VictoryBar labels not displaying correctly

I cannot get my VictoryBar charts to display labels correctly in my React Native application. The x-axis represents days and y-axis represents values. I would like the chart to display values above each bar, but it displays the day. I have set the labels property but it does nothing. Nothing happens when I console.log(d) as illustrated. The result of this code is below.

"react-native": "0.59.9" "victory-native": "^32.0.2" "react-native-svg": "^9.5.1",

Thanks

<VictoryChart domainPadding={70}>
  <VictoryBar 
     data={bars} 
     x="label"  
     y="value"
     // animate={{ onLoad: { duration: 1000 } }}
     style={{ data: { width: 20, fill: (d) => d.x === 3 ? "#000000" : "#49C6B7" }}}
     labels={(d)=>{console.log(d);return d.y}}
  />
  <VictoryAxis
    //x
    tickLabelComponent={<VictoryLabel angle={45} />}
    style={{
      axis: {stroke: 'grey'},
      ticks: {stroke: 'white'},
      tickLabels: {fontSize: 12, padding: 3, marginLeft:10, stroke:"white", verticalAnchor: "middle", textAnchor:'start'}
    }}
  />
  <VictoryAxis
  //y
    tickFormat={(d)=> numeral(d).format('0.0a')}
    dependentAxis
    style={{
    axis: {stroke: "grey"},
    grid: {stroke:'grey'},
    tickLabels: {fontSize: 0, padding: 0,   stroke:'white'}
    }}
  />
</VictoryChart> 

enter image description here

Upvotes: 1

Views: 4600

Answers (1)

Justin Joseph
Justin Joseph

Reputation: 3899

labels={({ datum }) => `${datum.y}`}
import { VictoryBar,VictoryChart,VictoryAxis,VictoryTheme } from "victory-native";
<>
      <VictoryChart
  domainPadding={{ x: 20 }}
>
  <VictoryBar
    data={[
      { x: "Year 1", y: 150 },
      { x: "Year 2", y: 250 },
      { x: "Year 3", y: 100 },
      { x: "Year 4", y: 750 },
      { x: "Year 5", y: 100 }
    ]}
    style={{
      data: { fill: "black", width: 12 }
    }}
    animate={{
      onExit: {
        duration: 500,
        before: () => ({
          _y: 0,
          fill: "orange",
          label: "BYE"
        })
      }
    }}
  />
  </VictoryChart>
<VictoryChart
    responsive={false}
    animate={{
      duration: 500,
      onLoad: { duration: 200 }
    }}
    domainPadding={{ x: 0 }}
    theme={VictoryTheme.material}
  >
    <VictoryAxis />
    <VictoryBar
      barRatio={1}
      cornerRadius={0} 
      style={{ data: { fill: "#6DB65B" } }}
      alignment="middle"
      labels={({ datum }) => `${datum.y}`} // <-- important
      data={[
        { x: "Year 1", y: 150 },
        { x: "Year 2", y: 250 },
        { x: "Year 3", y: 100 },
        { x: "Year 4", y: 750 },
        { x: "Year 5", y: 100 }
      ]}
    />
  </VictoryChart>
  </>

enter image description here

Upvotes: 4

Related Questions