Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control Victory x axis ticks labels

I'm using Victory to render a data set:

class App extends React.Component {
  render() {
    return (
      <div style={{ width: 600 }}>
        <VictoryChart domainPadding={30}>
          <VictoryAxis
            dependentAxis={true}
            style={{
              grid: { stroke: "grey" }
            }}
          />
          <VictoryAxis />
          <VictoryBar
            barWidth={20}
            style={{ data: { fill: "red" } }}
            data={[
              { x: new Date("2019-01-01"), y: 2 },
              { x: new Date("2019-02-01"), y: 3 },
              { x: new Date("2019-03-01"), y: 5 },
              { x: new Date("2019-04-01"), y: 4 },
              { x: new Date("2019-05-01"), y: 8 },
              { x: new Date("2019-06-01"), y: 2 },
              { x: new Date("2019-07-01"), y: 3 },
              { x: new Date("2019-08-01"), y: 5 },
              { x: new Date("2019-09-01"), y: 9 },
              { x: new Date("2019-10-01"), y: 3 },
              { x: new Date("2019-11-01"), y: 5 },
              { x: new Date("2019-12-01"), y: 6 }
            ]}
          />
        </VictoryChart>
      </div>
    );
  }
}

Screenshot

How can I change the x-axis to render a tick for each month ("jan", "feb", "mar" etc.)? Further, I would like each bar to have a width of 40 px (barWidth=40), but when I do that all bars are just merged - I would like to control the margin/padding between the bars as well. How should this be solved?

An example is available in this sandbox: https://codesandbox.io/s/victory-react-native-4t49q

like image 531
dhrm Avatar asked Sep 11 '19 13:09

dhrm


People also ask

How to change the appearance of the Axis tick mark labels?

Change the appearance of the axis tick mark labels The color, the font size and the font face of axis tick mark labels can be changed using the functions theme () and element_text () as follow : p + theme(axis.text.x= element_text(family, face, colour, size)) p + theme(axis.text.y = element_text(family, face, colour, size))

How to set axis ticks for discrete and continuous axes in R?

Set axis ticks for discrete and continuous axes. x or y axis can be discrete or continuous. In each of these two cases, the functions to be used for setting axis ticks are different. The functions scale_x_discrete() and scale_y_discrete() are used to customize discrete x and y axis, respectively.

What are the default labels for tick marks in R?

labels : labels of axis tick marks. Allowed values are : trans for axis transformations. Possible values are “log2”, “log10”, “sqrt”, etc The R code below set the position of tick marks on the y axis of the box plot.

How to set tick marks on the Y axis of boxplot?

The R code below set the position of tick marks on the y axis of the box plot. The function scale_y_continuous () and the argument breaks are used to choose where the tick marks appear : Tick mark labels can be formatted to be viewed as percents, dollars or scientific notation.


1 Answers

You may show labels as x-axis ticks. Then to control margin/padding you may set container's width a bit wider to fit bolder bars.

import React from "react";
import { render } from "react-dom";
import { VictoryChart, VictoryBar, VictoryAxis, VictoryContainer, VictoryLabel } from "victory";

class App extends React.Component {
  render() {

    let month = new Array(12);
    month[0] = "January";
    month[1] = "February";
    month[2] = "March";
    month[3] = "April";
    month[4] = "May";
    month[5] = "June";
    month[6] = "July";
    month[7] = "August";
    month[8] = "September";
    month[9] = "October";
    month[10] = "November";
    month[11] = "December";

    return (
      <div style={{ width: 600 }}>
        <VictoryChart domainPadding={30}
            width={900}
            containerComponent={<VictoryContainer responsive={false}/>}>
          <VictoryAxis
            dependentAxis={true}
            style={{
              grid: { stroke: "grey" }
            }}
          />
          <VictoryAxis 
            tickFormat={(x) => ``}
          />
          <VictoryBar
            barWidth={50}
            padding={{ left: 20, right: 60 }}
            style={{ data: { fill: "red" } }}
            data={[
              { x: new Date("2019-01-01"), y: 2 },
              { x: new Date("2019-02-01"), y: 3 },
              { x: new Date("2019-03-01"), y: 5 },
              { x: new Date("2019-04-01"), y: 4 },
              { x: new Date("2019-05-01"), y: 8 },
              { x: new Date("2019-06-01"), y: 2 },
              { x: new Date("2019-07-01"), y: 3 },
              { x: new Date("2019-08-01"), y: 5 },
              { x: new Date("2019-09-01"), y: 9 },
              { x: new Date("2019-10-01"), y: 3 },
              { x: new Date("2019-11-01"), y: 5 },
              { x: new Date("2019-12-01"), y: 6 }
            ]}
            labels={( datum ) => `${month[datum.x.getMonth()]}`}
            labelComponent={<VictoryLabel y={250} verticalAnchor={"start"}/>}
          />
        </VictoryChart>
      </div>
    );
  }
}

render(<App />, document.getElementById("root"));

As a sandbox: link

like image 130
Renat Avatar answered Sep 20 '22 13:09

Renat