javascript - 如何使用 highcharts 3D 散点图获得立方图?

标签 javascript highcharts 3d scatter-plot

我正在寻找一个使用 JavaScript 显示 3D 散点图的工具,并找到了 Highcharts' 3D scatter chart看起来不错。

唯一的问题是我希望图表具有立方体形式,因为所有轴都从 0 到 10。现在 X 轴的大小是其他轴的两倍,我找不到更改此设置的方法。

Here is Highcharts fiddle for the 3d scatter chart

$(function () {

    // Give the points a 3D feel by adding a radial gradient
    Highcharts.getOptions().colors = $.map(Highcharts.getOptions().colors, function (color) {
        return {
            radialGradient: {
                cx: 0.4,
                cy: 0.3,
                r: 0.5
            },
            stops: [
                [0, color],
                [1, Highcharts.Color(color).brighten(-0.2).get('rgb')]
            ]
        };
    });

    // Set up the chart
    var chart = new Highcharts.Chart({
        chart: {
            renderTo: 'container',
            margin: 100,
            type: 'scatter',
            options3d: {
                enabled: true,
                alpha: 10,
                beta: 30,
                depth: 250,
                viewDistance: 5,

                frame: {
                    bottom: { size: 1, color: 'rgba(0,0,0,0.02)' },
                    back: { size: 1, color: 'rgba(0,0,0,0.04)' },
                    side: { size: 1, color: 'rgba(0,0,0,0.06)' }
                }
            }
        },
        title: {
            text: 'Draggable box'
        },
        subtitle: {
            text: 'Click and drag the plot area to rotate in space'
        },
        plotOptions: {
            scatter: {
                width: 10,
                height: 10,
                depth: 10
            }
        },
        yAxis: {
            min: 0,
            max: 10,
            title: null
        },
        xAxis: {
            min: 0,
            max: 10,
            gridLineWidth: 1
        },
        zAxis: {
            min: 0,
            max: 10
        },
        legend: {
            enabled: false
        },
        series: [{
            name: 'Reading',
            colorByPoint: true,
            data: [[1, 6, 5], [8, 7, 9], [1, 3, 4], [4, 6, 8], [5, 7, 7], [6, 9, 6], [7, 0, 5], [2, 3, 3], [3, 9, 8], [3, 6, 5], [4, 9, 4], [2, 3, 3], [6, 9, 9], [0, 7, 0], [7, 7, 9], [7, 2, 9], [0, 6, 2], [4, 6, 7], [3, 7, 7], [0, 1, 7], [2, 8, 6], [2, 3, 7], [6, 4, 8], [3, 5, 9], [7, 9, 5], [3, 1, 7], [4, 4, 2], [3, 6, 2], [3, 1, 6], [6, 8, 5], [6, 6, 7], [4, 1, 1], [7, 2, 7], [7, 7, 0], [8, 8, 9], [9, 4, 1], [8, 3, 4], [9, 8, 9], [3, 5, 3], [0, 2, 4], [6, 0, 2], [2, 1, 3], [5, 8, 9], [2, 1, 1], [9, 7, 6], [3, 0, 2], [9, 9, 0], [3, 4, 8], [2, 6, 1], [8, 9, 2], [7, 6, 5], [6, 3, 1], [9, 3, 1], [8, 9, 3], [9, 1, 0], [3, 8, 7], [8, 0, 0], [4, 9, 7], [8, 6, 2], [4, 3, 0], [2, 3, 5], [9, 1, 4], [1, 1, 4], [6, 0, 2], [6, 1, 6], [3, 8, 8], [8, 8, 7], [5, 5, 0], [3, 9, 6], [5, 4, 3], [6, 8, 3], [0, 1, 5], [6, 7, 3], [8, 3, 2], [3, 8, 3], [2, 1, 6], [4, 6, 7], [8, 9, 9], [5, 4, 2], [6, 1, 3], [6, 9, 5], [4, 8, 2], [9, 7, 4], [5, 4, 2], [9, 6, 1], [2, 7, 3], [4, 5, 4], [6, 8, 1], [3, 4, 0], [2, 2, 6], [5, 1, 2], [9, 9, 7], [6, 9, 9], [8, 4, 3], [4, 1, 7], [6, 2, 5], [0, 4, 9], [3, 5, 9], [6, 9, 1], [1, 9, 2]]
        }]
    });


    // Add mouse events for rotation
    $(chart.container).bind('mousedown.hc touchstart.hc', function (e) {
        e = chart.pointer.normalize(e);

        var posX = e.pageX,
            posY = e.pageY,
            alpha = chart.options.chart.options3d.alpha,
            beta = chart.options.chart.options3d.beta,
            newAlpha,
            newBeta,
            sensitivity = 5; // lower is more sensitive

        $(document).bind({
            'mousemove.hc touchdrag.hc': function (e) {
                // Run beta
                newBeta = beta + (posX - e.pageX) / sensitivity;
                newBeta = Math.min(100, Math.max(-100, newBeta));
                chart.options.chart.options3d.beta = newBeta;

                // Run alpha
                newAlpha = alpha + (e.pageY - posY) / sensitivity;
                newAlpha = Math.min(100, Math.max(-100, newAlpha));
                chart.options.chart.options3d.alpha = newAlpha;

                chart.redraw(false);
            },
            'mouseup touchend': function () {
                $(document).unbind('.hc');
            }
        });
    });

});

有人知道如何解决这个问题吗?

最佳答案

这里有两种方法,我认为这两种方法都不理想,但是鉴于您的图表的外观(宽度和高度)有些静态,因此可以完成工作。

  1. 设置边距以使绘图区域反射(reflect)轴的尺寸。在你的情况下,x 和 y 为 1:1。除了相当静态且设置起来很奇怪之外,这几乎没有缺点。另请注意,您必须限制最小宽度,否则当浏览器宽度较小时,它会看起来很奇怪。使用分散示例,它可能如下所示 ( JSFiddle ):

    var chart = new Highcharts.Chart({
        chart: {
            renderTo: 'container',
            margin: 100,
            marginLeft: 300,
            marginRight: 300,
            // ...
        }
        // ...
    });
    
  2. 设置轴的宽度和高度。这种方法的问题宽度是图表将围绕一个不存在的“中点”旋转,并且它将被放置在图的左侧而无需进一步修改。再次使用散点图,它可能看起来像这样( JSFiddle ):

    var chart = new Highcharts.Chart({
        yAxis: {
            min: 0,
            max: 10,
            width: 200,
            height: 200,
            // ...
        },
        xAxis: {
            min: 0,
            max: 10,
            width: 200,
            height: 200,
            // ...
        },
        zAxis: {
            min: 0,
            max: 10,
            width: 200,
            height: 200,
            // ...
        }
        // ...
    });
    

关于javascript - 如何使用 highcharts 3D 散点图获得立方图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30190958/

相关文章:

javascript - underscore.template 的 _.templateSettings 的正确 mustache 模板配置是什么?

java - 如何将屏幕上的 3D 位置转换为 2D 点?

c++ - 使用opengl显示3D点

java四元数3D旋转实现

javascript - 为什么 npm install 没有更新我的 package.json?

javascript - Google People API 请求

javascript - 如何在不触发缓慢重绘的情况下在网页上移动 SVG?

javascript - 使用 ui-sref 插入 highcharts 创建动态链接

javascript - 如何在Highcharts的饼图中动态设置标题

javascript - Highstock - 更改输入范围超出数据最大值