React-native-chart-kit: How to set click function on chart ?

Created on 21 Mar 2019  路  10Comments  路  Source: indiespirit/react-native-chart-kit

I think this function should be important. I want to find out when user click on my chart. because I want to show more details.

Most helpful comment

@Foveluy Many thanks, but in my case: React.Fragment instead of React.fragment

All 10 comments

There is only a click handler for data points on the graph. Can you listen on the graph's parent component? Like, wrap the graph in a Touchable.

I want to catch user's click on linear chart graph.
I searched and I find a function: onDataPointClick . but It doesn't work:

<LineChart
      onDataPointClick={({value, dataset, getColor}) =>
      console.log('test:',value)
    }
    data={{
      labels: ['January', 'February', 'March', 'April', 'May', 'June'],
      datasets: [{
        data: [
          Math.random() * 100,
          Math.random() * 100,
          Math.random() * 100,
          Math.random() * 100,
          Math.random() * 100,
          Math.random() * 100
        ]
      }]
    }}
    width={Dimensions.get('window').width} // from react-native
    height={220}
    chartConfig={{
      backgroundColor: '#e26a00',
      backgroundGradientFrom: '#fb8c00',
      backgroundGradientTo: '#ffa726',
      decimalPlaces: 2, // optional, defaults to 2dp
      color: (opacity = 1) => `rgba(255, 255, 255, ${opacity})`,
      style: {
        borderRadius: 16
      }
    }}
    bezier

    style={{
      marginVertical: 8,
      borderRadius: 16
    }}


  />

Any solution?

Not at this moment... onDataPointClick only works for the dots on the graph, not the graphs themselves.

Not at this moment... onDataPointClick only works for the dots on the graph, not the graphs themselves.

ok, can you show me an example with dots?

https://github.com/indiespirit/react-native-chart-kit/blob/master/contributing.md

of course my code like example, but it doesn't work:

        <View>
          <LineChart
          withDots
          onDataPointClick={({value, getColor}) =>
                  showMessage({
                    message: `${value}`,
                    description: 'You selected this value',
                    backgroundColor: getColor(0.9)
                  })
                }
            data={{
              labels: ['January', 'February', 'March', 'April', 'May', 'June'],
              datasets: [{
                data: [
                  Math.random() * 100,
                  Math.random() * 100,
                  Math.random() * 100,
                  Math.random() * 100,
                  Math.random() * 100,
                  Math.random() * 100
                ]
              },
              {
                data: [
                  Math.random() * 100,
                  Math.random() * 100,
                  Math.random() * 100,
                  Math.random() * 100,
                  Math.random() * 100,
                  Math.random() * 100
                ]
              }]
            }}
            width={Dimensions.get('window').width} // from react-native
            height={Dimensions.get('window').height}
            chartConfig={{
              backgroundColor: '#e26a00',
              backgroundGradientFrom: '#fb8c00',
              backgroundGradientTo: '#ffa726',
              decimalPlaces: 2, // optional, defaults to 2dp
              color: (opacity = 1) => `rgba(255, 255, 255, ${opacity})`,
              style: {
                borderRadius: 16
              }
            }}
            style={{
            }}
          />
        </View>

the problem comes from here.

       output.push(
          <View key={Math.random()}>
            <Circle
              cx={cx}
              cy={cy}
              r="4"
              fill={this.getColor(dataset, 0.9)}
              onPress={onPress}
            />
            <Circle
              cx={cx}
              cy={cy}
              r="12"
              fill={this.getColor(dataset, 0)}
              onPress={onPress}
            />
          </View>
        )
      })
    })
    return output
  }

just simply edit the source code from View to React.Fragment, it will work and show the dots

  <React.Fragment key={Math.random()}>
            <Circle
              cx={cx}
              cy={cy}
              r="4"
              fill={this.getColor(dataset, 0.9)}
              onPress={onPress}
            />
            <Circle
              cx={cx}
              cy={cy}
              r="12"
              fill={this.getColor(dataset, 0)}
              onPress={onPress}
            />
</React.Fragment>

@Hermanya The dots will not show in react-native ^0.59.0, i think the problem comes from the SVG lib, but not sure.

by changing View to React.Fragment can work again.

@Foveluy Many thanks, but in my case: React.Fragment instead of React.fragment

What @Hermanya said works. To achieve chart click function, just wrap the graph in a touchable component, like this:
<TouchableWithoutFeedback onPress={()=>console.log('click')}> <View> <LineChart bezier data={{ labels: labels, datasets: [{data: data}] }} width={Dimensions.get("window").width height={360} // ... /> </View> <TouchableWithoutFeedback />

Was this page helpful?
0 / 5 - 0 ratings

Related issues

LillyBrainy picture LillyBrainy  路  4Comments

azui007 picture azui007  路  6Comments

bailih picture bailih  路  4Comments

trungtin2202 picture trungtin2202  路  3Comments

Ivanrs297 picture Ivanrs297  路  6Comments