Cube.js Blog

React Native Charts with Cube.js and Victory

Author avatarTimur Minulin/Tutorials/April 24, 2019
React Native Charts with Cube.js and Victory
Show Original

Giving mobile users access to analytical data is always a hard problem to solve. Browsing a heavy website on a small screen usually is not the best user experience. Building native mobile apps is a great solution, but usually requires a lot of effort.

React-Native makes building and maintaining native applications much easier. By coupling it with Cube.js and Victory-Native, I'll show you how to build an analytics dashboard embedded into a native mobile app. The resulting app can run both on iOS and Android; you can try it out by using the Expo app on your own device.

Below is a screenshot of the final app. Here's the app snack on Expo—you can run it online or launch it on your device. Also, there you can find all the source code from this tutorial.

The data

We are going to use the Cube.js backend with sample data from Nginx logs. I’ve covered how to collect Nginx logs and analyze them with AWS Athena and Cube.js in this tutorial. Let’s recap the data schema we’re going to query:

cube(`Logs`, {
  measures: {
    count: {
      type: `count`,
    },

    errorCount: {
      type: `count`,
      filters: [
        { sql: `${CUBE.isError} = 'Yes'` }
      ]
    },

    errorRate: {
      type: `number`,
      sql: `100.0 * ${errorCount} / ${count}`,
      format: `percent`
    }
  },

  dimensions: {
    status: {
      sql: `status`,
      type: `number`
    },

    isError: {
      type: `string`,
      case: {
        when: [{
          sql: `${CUBE}.status >= 400`, label: `Yes`
        }],
        else: { label: `No` }
      }
    },

    createdAt: {
      sql: `from_unixtime(created_at)`,
      type: `time`
    }
  }
});

Cube.js uses data schemas like the one above to generate SQL and execute it against your database. If you are new to Cube.js, I recommend checking this Cube.js 101 tutorial.

React-Native

To build a react-native app, we'll be using the react-native-cli package. Go ahead and install it:

npm install -g react-native-cli
# or
yarn global add react-native-cli

Now you can create a new app:

react-native init cubejs-rn-demo
cd cubejs-rn-demo

This will create a barebones react-native app.

Cube.js

Cube.js provides a client package for loading data from the backend:

npm install -s @cubejs-client/core
# or
yarn add @cubejs-client/core

It works for both web and native apps. Also, Cube.js has a React component, which is easier to work with:

npm install -s @cubejs-client/react
# or
yarn add @cubejs-client/react

The Cube.js React client also works great with React-Native. The client itself doesn't provide any visualisations and is designed to work with existing chart libraries. It provides a set of methods to access Cube.js API and to work with query result.

Victory

For charts, we will be using the victory-native library.

npm install -s victory-native
# or
yarn add victory-native

Now we can create a simple pie chart just like on the demo dashboard. Here's the code:

<VictoryChart width={this.state.width}>
  <VictoryPie
    data={data.chartPivot()}
    y="Logs.count"
    labels={item => numberFormatter(item[data.seriesNames()[0].key])}
  />
</VictoryChart>

Building a Dashboard

We are going to build a simple dashboard with just a couple of tiles, so we'll use a ScrollView. If you're going to have many tiles on a dashboard, it would be better to switch to a FlatList because of potential performance issues. So, let's create a simple dashboard. First, we are going to create a Chart component, where we'll define all required data.

const Empty = () => <Text>No component for that yet</Text>;

const chartElement = (type, data) => {
  switch (type) {
    case 'line':
      return <LineChart data={data} />;
    case 'pie':
      return <PieChart data={data} />;
    case 'bar':
      return <BarChart data={data} />;
    default:
      return <Empty />;
  }
};

const Chart = ({ type }) => (
  <QueryRenderer
    query={queries[type]}
    cubejsApi={cubejsApi}
    render={({ resultSet }) => {
      if (!resultSet) {
        return <ActivityIndicator size="large" color="#0000ff" />;
      }

      return chartElement(type, resultSet);
    }}
  />
);

Also, we will be using Victory's zoomContainer to allow users to zoom into the data. We'll also save current device orientation to add more data in landscape mode and change paddings:

const padding = {
    portrait: { left: 55, top: 40, right: 45, bottom: 50 },
    landscape: { left: 100, top: 40, right: 70, bottom: 50 }
};

const tickCount = {
  portrait: 4,
  landscape: 9
};

export const colors = [
  "#7DB3FF",
  "#49457B",
  "#FF7C78",
  "#FED3D0",
  "#6F76D9",
  "#9ADFB4",
  "#2E7987"
];

class ChartWrapper extends React.Component {
  constructor() {
    super();
    this.state = { orientation: 'portrait', ...Dimensions.get('window')};
    this.updateDimensions = this.updateDimensions.bind(this);
  }

  componentDidMount() {
    this.updateDimensions();
  }

  handleZoom(domain) {
    this.setState({ selectedDomain: domain });
  }

  updateDimensions() {
    const windowSize = Dimensions.get('window');
    const orientation = windowSize.width < windowSize.height ? 'portrait' : 'landscape';
    this.setState({ orientation, ...windowSize });
  }

  render() {
    return (
      <View style={vStyles.container} onLayout={this.updateDimensions}>
        <VictoryChart
          width={this.state.width}
          padding={padding[this.state.orientation]}
          domainPadding={{x: 10, y: 25}}
          colorScale={colors}
          tickCount={4}
          containerComponent={
            <VictoryZoomContainer responsive={true}
              zoomDimension="x"
              zoomDomain={this.state.zoomDomain}
              onZoomDomainChange={this.handleZoom.bind(this)}
            />
          }
        >
          {this.props.children}
          {!this.props.hideAxis &&
            <VictoryAxis tickCount={tickCount[this.state.orientation]} />
          }
          {!this.props.hideAxis &&
            <VictoryAxis dependentAxis />
          }
        </VictoryChart>
      </View>
    );
  }
}

Please note that the default app.json config has locked portrait screen orientation. To allow device rotation, set "orientation" to "default"—that will allow all orientations except upside down. This code scales the charts when the device is rotated:

Landscape view

Let's start with a line chart. First, we'll need to define a basic Cube.js query in Chart.js to get the data:

{
  measures: ["Logs.errorRate"],
  timeDimensions: [
    {
      dimension: "Logs.createdAt",
      dateRange: ["2019-04-01", "2019-04-09"],
      granularity: "day"
    }
  ]
}

Now we can create a LineChart component. It's a basic Victory chart with a bit of styling:

const LineChart = ({ data }) => (
  <ChartWrapper>
    <VictoryLine
      data={data.chartPivot()}
      x={dateFormatter}
      labels={null}
      y={data.seriesNames()[0].key}
      style={{
        data: { stroke: "#6a6ee5" },
        parent: { border: "1px solid #ccc"}
      }}
    />
  </ChartWrapper>
);

We can include this component in the Chart.js file and render in the Dashboard.js screen:

const Dashboard = () => {
  return (
    <ScrollView>
      <View style={styles.item}>
        <Text style={styles.text}>Error Rate by Day</Text>
        <Chart type="line" />
      </View>
    </ScrollView>
  );
};

The same applies to Stacked Bar Chart. The only complication is that it consists of multiple series, so we add a bar for each series and make a legend:

const BarChart = ({ data }) => (
  <ChartWrapper>
    <VictoryStack colorScale={colors}>
      {data.seriesNames().map((series, i) => (
        <VictoryBar
          key={i}
          x={dateFormatter}
          y={series.key.replace(":", ", ")}
          data={data.chartPivot()}
          labels={null}
          style={{
            parent: { border: "1px solid #ccc"}
          }}
        />
      ))}
    </VictoryStack>
    <VictoryLegend x={40} y={280}
      orientation="horizontal"
      colorScale={colors}
      data={data.seriesNames().map(({ title }) => ({ name: title.substring(0, 3) }))}
    />
  </ChartWrapper>
);

Now we come to the pie chart. There's a trick to hide the axis—we add an empty VictoryAxis here:

const PieChart = ({ data }) => (
  <ChartWrapper hideAxis>
    <VictoryPie
      data={data.chartPivot()}
      y="Logs.count"
      labels={item => numberFormatter(item[data.seriesNames()[0].key])}
      padAngle={3}
      innerRadius={40}
      labelRadius={70}
      style={{ labels: { fill: "white", fontSize: 14 } }}
      colorScale={colors}
    />
    <VictoryAxis style={{ axis: { stroke: "none" }, tickLabels: { fill: "none" } }} />
    <VictoryLegend x={40} y={260}
      orientation="horizontal"
      colorScale={colors}
      data={data.chartPivot().map(({ x }) => ({ name: x }))}
    />
  </ChartWrapper>
);

Here's a screenshot of the pie chart on the dashboard:

And our dashboard is done! You can find all the code and the app on Expo. You can run this app online or launch it on your device via the Expo app. It will work relatively slow as it's not compiled with platform-specific native code, but you can always download the source code and build a native app for your platform via Xcode or Android Studio.

I hope this tutorial helps you build great apps!

You may also like: