简体   繁体   中英

Google Charts - ScatterChart Tick Marks and Styling

I created a ScatterChart to visualize relationships between different data-fields. The visualized data can be numeric or nominal/ordinal in nature. The nominal/ordinal values are therefore mapped to numeric values. A simple example would be "Age + Gender", where gender is nominal and I map 'male' => 1, 'female ' => 2 to get the desired output.

目前发生的例子

So far so good, chart is working, but I need help formatting it.

As you can see the first gender column is displayed on the y axis and the second one at the far right side of the graph. I would like them to be layed out "pretty" as in some space to the y-axis, some space to the right end.

Also I would like to display the appropriate tick marks for "male" and "female" on the y-axis.

EXTENSION:

I also want to split the data into different series to be able to colorize eg 'male' and 'female' data points in different colors.

What I do is build 3 columns in my data-table (age, male, female), but I can't quite get it to output correctly. Currently it's all merging into one displayed column.

Here's my Code so far:

var drawMe = function(){
       var columns = 0;
       var xColumns = 0;
       var yColumns = 0;
       var gdata = new google.visualization.DataTable();
       /**
        * data Object:
        * data.xName = Name of x parameter
        * data.yName = Name of y parameter
        * data.x = data for x paramter
        * data.y = data for y parameter
        * data.xType = type of x parameter, either num (= number) or other (= string)
        * data.yType = type of y parameter, either num (= number) or other (= string)
        * data.xChoices = array of strings representing availble choices for x if xType !== num
        * daty.yChoices = array of strings representing availble choices for y if yType !== num
        * 
        */
       if(data.xType === 'num'){
           gdata.addColumn('number', data.xName);
           xColumns++;
           columns++;
       } else {
           for(var i = 0; i < data.xChoices.length; i++){
               gdata.addColumn('number', data.xChoices[i]);
               xColumns++;
               columns++;
           }
       }
       if(data.yType === 'num'){
           gdata.addColumn('number', data.yName);
           yColumns++;
           columns++;
       } else {
           for(var i = 0; i < data.yChoices.length; i++){
               gdata.addColumn('number', data.yChoices[i]);
               columns++;
               yColumns++;
           }
       }
       var x;
       var y;
       for(var i = 0; i < count; i++){ // count is set by closure, cause data is paged via ajax
           // initialize zero row
           var row = [];
           for(var j = 0; j < columns; j++){
               row[j] = null;
           }
           if(data.xType === 'num'){
               x = parseFloat(data.x[i]);
               row[0] = x;
           } else {
               var index = data.xChoices.indexOf(data.x[i]);
               x = {
                   v: index + 1, // don't start at 0
                   f: data.xChoices[index],
               };
               row[index] = x;
           }
           if(data.yType === 'num'){
               y = parseFloat(data.y[i]);
               row[xColumns] = y;
           } else {
               var index = data.yChoices.indexOf(data.y[i]);
               y = {
                   v: index + 1, // don't start at 0
                   f: data.yChoices[index],
               };
               row[xColumns + index] = y;
           }
           gdata.addRow(row);
       }
       var xTitle = data.xName;
       if(data.xUnit){
           xTitle += ' [' + data.xUnit + ']';
       }
       var yTitle = data.yName;
       if(data.yUnit){
           yTitle += ' [' + data.yUnit + ']';
       }
       var xGridLines = -1;
       var yGridLines = -1;
       var xTicks = false;
       var yTicks = false;
       if(data.xType !== 'num' && data.xChoices){
           xGridLines = data.xChoices.length + 2;
           xTicks = [{v: 0, f: ''}]; // empty tick at the beginning
           for(var i = 0; i < data.xChoices.length; i++){
               xTicks.push({v: i+1, f: data.xChoices[i]});
           }
           xTicks.push({v: 3, f: ''}); // empty tick at the end
       }
       if(data.yType !== 'num' && data.yChoices){
           yGridLines = data.yChoices.length + 2;
           yTicks = [{v: 0, f: ''}];
           for(var i = 0; i < data.yChoices.length; i++){
               yTicks.push({v: i+1, f: data.yChoices[i]});
           }
           yTicks.push({v: 3, f: ''});
       }
       var options = {
         title: data.xName + ' vs. ' + data.yName,
         hAxis: {
             title: xTitle,
             gridlines: {
                 count: xGridLines
             }
         },
         vAxis: {
             title: yTitle,
             gridlines: {
                 count: yGridLines
             }
         }
       };
       if(xTicks !== false){
           options.hAxis.ticks = xTicks;
           options.hAxis.viewWindowMode = 'pretty';
       }
       if(yTicks !== false){
           options.vAxis.ticks = yTicks;
           options.vAxis.viewWindowMode = 'pretty';
       }
       options.series = {};
       for(var i = 0; i < columns; i++){
           options.series[i] = {color: atk.COLORS[i], visibleInLegend: true};
       }
       var chart = new google.visualization.ScatterChart(element);
       chart.draw(gdata, options);
   };

Thanks for your help and time!

You're in luck, there is an update to the API that is in the RC version (1.1) that should be able to do exactly what you want. Load the RC version first:

google.load('visualization', '1.1', {packages: ['corechart']});

Then set the hAxis.ticks option like this:

hAxis: {
    ticks: [{v: 0, f: ''}, {v: 1, f: 'Male'}, {v: 2, f: 'Female'}, {v: 3, f: ''}]
}

Try that and see if it works.

Edit:

To make the males and females different colors, you need to split the data into two separate columns. You can accomplish this by using a DataView:

// assumes Male/Female is column 0 and age is column 1 in your DataTable
var view = new google.visualization.DataView(data);
view.setColumns([0, {
    type: 'number',
    label: 'Age',
    calc: function (dt, row) {
        // return a value only if male
        return (dt.getValue(row, 0) == 1) ? dt.getValue(row, 1) : null;
    }
}, {
    type: 'number',
    label: 'Age',
    calc: function (dt, row) {
        // return a value only if female
        return (dt.getValue(row, 0) == 2) ? dt.getValue(row, 1) : null;
    }
}]);

Draw the chart using the DataView instead of the DataTable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM