简体   繁体   English

Google Charts - ScatterChart刻度标记和样式

[英]Google Charts - ScatterChart Tick Marks and Styling

I created a ScatterChart to visualize relationships between different data-fields. 我创建了一个ScatterChart来可视化不同数据字段之间的关系。 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. 一个简单的例子是“年龄+性别”,其中性别是名义上的,我映射'男性'=> 1,'女性'=> 2以获得所需的输出。

目前发生的例子

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. 如您所见,第一个性别列显示在y轴上,第二个性别列显示在图表的最右侧。 I would like them to be layed out "pretty" as in some space to the y-axis, some space to the right end. 我希望它们在y轴的某个空间中“漂亮”,在右端有一些空间。

Also I would like to display the appropriate tick marks for "male" and "female" on the y-axis. 另外,我想在y轴上显示“男性”和“女性”的适当刻度线。

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. 我所做的是在我的数据表(年龄,男性,女性)中构建3列,但我无法正确输出它。 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. 您很幸运,RC版本(1.1)中的API有一个更新,应该能够完全按照您的需要进行操作。 Load the RC version first: 首先加载RC版本:

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

Then set the hAxis.ticks option like this: 然后像这样设置hAxis.ticks选项:

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: 您可以使用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. 使用DataView而不是DataTable绘制图表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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