Giter Club home page Giter Club logo

redcap_chart_field's Introduction

REDCap Chart Field

Provides a chart drawing feature for data entries and surveys. Integrates REDCap with third-party chart libraries - currently Charts.js and Chartist are supported. Piping can be used on field configuration, so charts may display facts to survey participants based on their previous answers.

Prerequisites

  • REDCap >= 8.0.3

Installation

  • Clone this repo into <redcap-root>/modules/redcap_chart_field_v<version_number>.
  • Go to Control Center > Manage External Modules and enable REDCap Chart Field.

Choosing your chart library

Two amazing third-party chart libraries are supported by this module - Chart.js and Chartist.

Configuration screen

Setting up a chart field (Chart.js example)

This section will walk you through a few steps to setup a Charts.js chart, dynamically populated via Piping.

1. Setting up source data (for Piping purposes)

The following fields will be used to provide data to populate our chart example. Note the variable names - they will be referenced later on.

Chart.js source data configuration

To configure a chart it helps to have data in the input fields. For this example we'll use the data shown here.

Chart.js source data

2. Creating a chart field

To create a chart field, access Online Designer's field creation modal, and then select Descriptive Text. You may see a checkbox named Is chart.

Field type

3. Mapping an example provided by the selected third-party library

Once you click on Is chart checkbox, a few extra fields are shown:

Chart.js configuration

To fill these fields out it is very important to get familiar with the documentation provided by the third-party chart library you chose. The inputs are expected to be Javascript objects that follow a particular structure defined by the library. The better you understand the configuration syntax the richer your charts can be.

Though this sounds very technical, no software development skills are required to manipulate these parameters. For most of cases you need only copy & paste a suitable example from documentation. Then make slight changes to adapt it to your needs.

Let's create a bar chart by using the main example from Chart.js official documentation as base.

Click to see the code provided by Chart.js website
<canvas id="myChart" width="400" height="400"></canvas>
<script>
var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
        datasets: [{
            label: '# of Votes',
            data: [12, 19, 3, 5, 2, 3],
            backgroundColor: [
                'rgba(255, 99, 132, 0.2)',
                'rgba(54, 162, 235, 0.2)',
                'rgba(255, 206, 86, 0.2)',
                'rgba(75, 192, 192, 0.2)',
                'rgba(153, 102, 255, 0.2)',
                'rgba(255, 159, 64, 0.2)'
            ],
            borderColor: [
                'rgba(255,99,132,1)',
                'rgba(54, 162, 235, 1)',
                'rgba(255, 206, 86, 1)',
                'rgba(75, 192, 192, 1)',
                'rgba(153, 102, 255, 1)',
                'rgba(255, 159, 64, 1)'
            ],
            borderWidth: 1
        }]
    },
    options: {
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero:true
                }
            }]
        }
    }
});
</script>
 

Note the sections above labeled type, data, and options. Those map to the REDCap Chart Field properties Chart type, Chart data, and Chart options, respectively. The canvas_width and canvas_height fields map to the Chart width and Chart height REDCap Chart Field properties.

1. Type: Bar
2. Data:

{
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3],
        backgroundColor: [
            'rgba(255, 99, 132, 0.2)',
            'rgba(54, 162, 235, 0.2)',
            'rgba(255, 206, 86, 0.2)',
            'rgba(75, 192, 192, 0.2)',
            'rgba(153, 102, 255, 0.2)',
            'rgba(255, 159, 64, 0.2)'
        ],
        borderColor: [
            'rgba(255,99,132,1)',
            'rgba(54, 162, 235, 1)',
            'rgba(255, 206, 86, 1)',
            'rgba(75, 192, 192, 1)',
            'rgba(153, 102, 255, 1)',
            'rgba(255, 159, 64, 1)'
        ],
        borderWidth: 1
    }]
}

3. Options:

{
    scales: {
        yAxes: [{
            ticks: {
                beginAtZero:true
            }
        }]
    }
}

4. Canvas width: 400
5. Canvas height: 400

4. Setting up chart properties

Here's how those values look pasted into the REDCap Chart Field properties.

Chartjs configuration

Note the highlighted area on image above - Piping is being used in order to dynamically populate the chart data ([data_1], [data_2], etc) - so each record entry or survey has its own chart results.

Obs.: as the example above, Piping wildcards must be used between quotes (single or double), e.g. '[data_1]'.

Note also that piping data from a REDCap field is optional. You could present a chart with static data or use a mix of static and piped data.

5. The result

For this particular case - recall the input given on Chart source data section - we have the following result chart when accessing the field in a survey:

Chartjs chart

Analogous example using Chartist

In this section we will go through the analogous example for Chartist library, using the same source data. This time we'll draw a line chart.

The process of mapping a Chartist example is similar to the previous section on Chart.js

Click to see the code provided by Chartist website
/* Add a basic data series with six labels and values */
var data = {
  labels: ['1', '2', '3', '4', '5', '6'],
  series: [
    {
      data: [1, 2, 3, 5, 8, 13]
    }
  ]
};

/* Set some base options (settings will override the default settings in Chartist.js *see default settings*). We are adding a basic label interpolation function for the xAxis labels. */
var options = {
  axisX: {
    labelInterpolationFnc: function(value) {
      return 'Calendar Week ' + value;
    }
  }
};

/* Now we can specify multiple responsive settings that will override the base settings based on order and if the media queries match. In this example we are changing the visibility of dots and lines as well as use different label interpolations for space reasons. */
var responsiveOptions = [
  ['screen and (min-width: 641px) and (max-width: 1024px)', {
    showPoint: false,
    axisX: {
      labelInterpolationFnc: function(value) {
        return 'Week ' + value;
      }
    }
  }],
  ['screen and (max-width: 640px)', {
    showLine: false,
    axisX: {
      labelInterpolationFnc: function(value) {
        return 'W' + value;
      }
    }
  }]
];

/* Initialize the chart with the above settings */
new Chartist.Line('#my-chart', data, options, responsiveOptions);
 

Again, you don't need to be a developer to manipulate this information. You need only to copy and paste the correct sections of some example code into the appropriate form fields and make some minor edits:

1. Type: Line
2. Data:

{
  labels: ['1', '2', '3', '4', '5', '6'],
  series: [
    {
      data: [1, 2, 3, 5, 8, 13]
    }
  ]
}

3. Options:

{
  axisX: {
    labelInterpolationFnc: function(value) {
      return 'Calendar Week ' + value;
    }
  }
}

3. Responsive options:

[
  ['screen and (min-width: 641px) and (max-width: 1024px)', {
    showPoint: false,
    axisX: {
      labelInterpolationFnc: function(value) {
        return 'Week ' + value;
      }
    }
  }],
  ['screen and (max-width: 640px)', {
    showLine: false,
    axisX: {
      labelInterpolationFnc: function(value) {
        return 'W' + value;
      }
    }
  }]
]

Filling out the form:

Chartist configuration

As we've done on Chart.js section, we are using Piping to make chart data dynamic.

Note that the configuration for Chartist is slightly different - there are no canvas dimension fields, and Chart responsive options field is added. Check the official Chartist documentation to understand how it works.

In our example, width and height are specified to the Chart options field to show that is also possible to define chart dimensions with Chartist.

The Result

Here is a line chart drawn using Chartist

Chartist line chart

redcap_chart_field's People

Contributors

tbembersimeao avatar pbchase avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.