简体   繁体   English

图表js Backgroundimage Scaling

[英]Chart js Backgroundimage Scaling

Hi i'm quite new to JS and looking for an easy way to show a custom map with my own backgroundimage and several points with xy-coordinates on it. 嗨,我对JS很新,并且正在寻找一种简单的方法来显示带有我自己的backgroundimage的自定义地图和带有xy坐标的几个点。

At first it worked quite well using the "line"-chart-type of chart.js (v2.5) with disabled showScale()-option and fixed axes-max/min-values. 最初使用“line”图表类型的chart.js(v2.5)和禁用的showScale() - 选项和固定的轴 - 最大/最小值,效果很好。

My problem is that the backgroundimage is not rescaling properly when i change the window-size or show/hide the legend of the chart. 我的问题是当我更改窗口大小或显示/隐藏图表的图例时,backgroundimage没有正确重新缩放。

Here is my JS-code for creating the chart: 这是我用于创建图表的JS代码:

function initMap() {
    scatterChart = new Chart(document.getElementById("scatterChart"), {
        responsive: true,
        type: 'line',
        data: {
            /*datasets: [
                {

                }
            ]
            */
        },
        showScale: false,
        options: {
            scales: {
                yAxes: [{
                    display: false,
                    ticks: {
                        max: y_max,
                        min: 0,
                        stepSize: 0.1
                    }
            }],
                xAxes: [{
                    display: false,
                    type: 'linear',
                    position: 'bottom',
                    ticks: {
                        max: x_max,
                        min: 0,
                        stepSize: 0.1
                    }
            }]
            },
            onClick: function (evt) {
                var activePoint = scatterChart.getElementAtEvent(evt);
            },
            legend: {
                position: 'right',
                labels: {
                    fontSize: 15
                }
            },
        }
    });
}

HTML: HTML:

<canvas id="scatterChart" style="width: 1920px; height: 1080px; background-image:url('media/map_background.jpg'); background-size: 100% 100%;"></canvas>

Here are two images showing the effect: 这是两个显示效果的图像:

https://1drv.ms/i/s!As59C5Mmd0nahr1zP4rjwI5KobaM3Q https://1drv.ms/i/s!As59C5Mmd0nahr1zP4rjwI5KobaM3Q

https://1drv.ms/i/s!As59C5Mmd0nahr10KwUCJ4wpvfn4_g https://1drv.ms/i/s!As59C5Mmd0nahr10KwUCJ4wpvfn4_g

What can i do to get a proper rescaling (points should always be shown on the same position on the map regardless of windowsize or enabled/disabled legend)? 我该怎么做才能获得适当的重新缩放(无论是windowsize还是启用/禁用图例,点都应该始终显示在地图上的相同位置)?

Have you tried using the maintainAspectRatio property and setting it to true ? 您是否尝试过使用maintainAspectRatio属性并将其设置为true That should hopefully fix your window resize issue. 这应该有希望修复你的窗口调整大小问题。

The only way to fix the legend issue is to use a legend that is external to the canvas because the legend takes up space in the canvas so the chart is squished horizontally (there is no layering concept in a canvas ). 解决图例问题的唯一方法是使用canvas外部的图例,因为图例占据canvas空间,因此图表会水平压扁( canvas没有分层概念)。 See this example for how to generate an external legend. 有关如何生成外部图例,请参阅此示例

You must use the legendCallback property and the .generateLegend() prototype method. 您必须使用legendCallback属性和.generateLegend()原型方法。

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

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