DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
What's in store for DevOps in 2023? Hear from the experts in our "DZone 2023 Preview: DevOps Edition" on Fri, Jan 27!
Save your seat
  1. DZone
  2. Coding
  3. JavaScript
  4. Data Viz Tutorial: React Native Charts With Cube.js and Victory

Data Viz Tutorial: React Native Charts With Cube.js and Victory

Learn how to make data visualization dashboards for mobile devices using React Native and two interesting JavaScript libraries.

Timur Minulin user avatar by
Timur Minulin
·
Apr. 30, 19 · Tutorial
Like (2)
Save
Tweet
Share
10.05K Views

Join the DZone community and get the full member experience.

Join For Free

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.

React Native App

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 visualizations 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:

React Native App

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:

React Native App

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!

React Native Data (computing) Chart React (JavaScript library) mobile app

Published at DZone with permission of Timur Minulin. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Cut the Release Inspection Time From 4 Days to 4 Hours
  • Internal Components of Apache ZooKeeper and Their Importance
  • Handling Automatic ID Generation in PostgreSQL With Node.js and Sequelize
  • AIOps Being Powered by Robotic Data Automation

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: