Search code examples
react-nativevictory-charts

Victory Chart X-Axis label should be group by week and prevent overlapping


I'm working on a react native application,

I have created a simple VictoryLine chart. At the X-Axis, I have dates and Y-Axis total amount of cash flow, the dates at the X-Axis are overlapping and Y-Axis values are to long to display. I have attached expected image and actual image. Please help me to resolve this issue.

Expected Image : enter image description here

Actual Image : enter image description here

My code as follows :

<View style={styles.chartContainer}>
        {cashFlowChartData == null ||
          cashFlowChartData.data == null ||
          cashFlowChartData.messageCode.code !== 1 ? (
            <TouchableOpacity
              onPress={() => setRefreshChart(refreshChart + 1)}>
              <Text style={styles.text5}>
                {i18n.t('cashFlowWidget.NoDataAvailableTryAgain')}
              </Text>
            </TouchableOpacity>
          ) : (
            <TouchableOpacity
              style={styles.graph}
              onLayout={(event) => onLayout(event)}>
              <View pointerEvents="none">
                {cashFlowChartData !== null ? (
                  <VictoryChart
                    theme={VictoryTheme.material}
                    width={chartSize.widthChart + 10}
                    height={250}>
                    {/* Red annotation line */}
                    <VictoryAxis
                      scale={{ x: "date" }}
                      tickValues={xTickValues}
                      // tickFormat={t => new Date(t)}
                    />
                    <VictoryAxis dependentAxis />
                    <VictoryLine
                      data={cashFlowChartData.data.cashOutChart}
                      // domain={{
                      //   x: [new Date(2021, 1, 1), new Date(2021, 1, 30)],
                      //   y: [0, 20]
                      // }}
                      interpolation="monotoneX"
                      scale={{ x: "time", y: "linear" }}
                      standalone={false}
                      style={{
                        data: { stroke: 'red' },
                        parent: { border: '1px solid #ccc' },
                      }} />

                    <VictoryLine
                      data={cashFlowChartData.data.cashInChart}
                      // domain={{
                      //   x: [new Date(2021, 1, 1), new Date(2021, 1, 30)],
                      //   y: [0, 20]
                      // }}
                      interpolation="monotoneX"
                      scale={{ x: "time", y: "linear" }}
                      standalone={false}
                      style={{
                        data: { stroke: 'green' },
                        parent: { border: '1px solid #ccc' },
                      }} />

                  </VictoryChart>
                ) : null}
              </View>
            </TouchableOpacity>
          )}
      </View>

Sample Data set :

{"messageCode":{"code":1,"message":"Success"},"data":{"cashInChart":[{"x":"2021-02-23","y":88136.39},{"x":"2021-02-24","y":3136.39},{"x":"2021-02-25","y":42442329.14},{"x":"2021-02-26","y":46631639.89},{"x":"2021-02-27","y":42020950.64},{"x":"2021-02-28","y":47379660.39},{"x":"2021-03-01","y":24370.14},{"x":"2021-03-02","y":596.39},{"x":"2021-03-03","y":822.64},{"x":"2021-03-04","y":49033048.89},{"x":"2021-03-05","y":48584275.14},{"x":"2021-03-06","y":50198914.64},{"x":"2021-03-07","y":51917656.64},{"x":"2021-03-08","y":52657548.64},{"x":"2021-03-09","y":70887.39},{"x":"2021-03-10","y":59284226.14},{"x":"2021-03-11","y":04554.89},{"x":"2021-03-12","y":56924883.64},{"x":"2021-03-13","y":72040.39},{"x":"2021-03-14","y":307.14},{"x":"2021-03-15","y":57944825.89},{"x":"2021-03-16","y":44.64},{"x":"2021-03-17","y":60319624.14},{"x":"2021-03-18","y":61517903.64},{"x":"2021-03-19","y":28652.64},{"x":"2021-03-20","y":32690.64},{"x":"2021-03-21","y":36728.64},{"x":"2021-03-22","y":0766.64},{"x":"2021-03-23","y":40758.64}],"cashOutChart":[{"x":"2021-02-23","y":0},{"x":"2021-02-24","y":0},{"x":"2021-02-25","y":0},{"x":"2021-02-26","y":0},{"x":"2021-02-27","y":0},{"x":"2021-02-28","y":0},{"x":"2021-03-01","y":0},{"x":"2021-03-02","y":0},{"x":"2021-03-03","y":0},{"x":"2021-03-04","y":0},{"x":"2021-03-05","y":0},{"x":"2021-03-06","y":0},{"x":"2021-03-07","y":0},{"x":"2021-03-08","y":0},{"x":"2021-03-09","y":0},{"x":"2021-03-10","y":0},{"x":"2021-03-11","y":0},{"x":"2021-03-12","y":0},{"x":"2021-03-13","y":0},{"x":"2021-03-14","y":0},{"x":"2021-03-15","y":0},{"x":"2021-03-16","y":0},{"x":"2021-03-17","y":0},{"x":"2021-03-18","y":0},{"x":"2021-03-19","y":500.00},{"x":"2021-03-20","y":0},{"x":"2021-03-21","y":0},{"x":"2021-03-22","y":10.00},{"x":"2021-03-23","y":0}],"id":0}}

Solution

  • Try use fixLabelOverlap={true}

    in <VictoryAxis> properties

    Like:

    <VictoryAxis style={{ tickLabels: { angle: 0 } }} fixLabelOverlap={true} />