自定义水平轴上的刻度/日期 – 通过wpDataTables使用Google Charts

huangapple go评论70阅读模式
英文:

Customzing ticks/dates on horizontal axis - Google Charts via wpDataTables

问题

我正在通过WP插件wpDataTables(wpDataCharts)在我的网站上插入Google折线图。我正在使用Elementor来构建网站。您可以在页面下方看到图表,链接在这里

wpDataTables只提供了有限数量的图表选项,您可以根据Google API的规定进行调整。如果您想进一步进行更改,可以使用回调函数。

我已经使用回调函数来移除Google默认添加的填充(这使得图表只填充容器的50%),并且使工具提示包括两个图形。工作代码如下:

<script type="text/javascript">
jQuery(window).on('load',function(){ 
    if( typeof wpDataChartsCallbacks == 'undefined' ){ wpDataChartsCallbacks = {}; } 
    wpDataChartsCallbacks[4] = function(obj){ 
        obj.options.chartArea = {left:135,top:30,width:'100%',height:'75%'};
        obj.options.focusTarget = ['category'];
    }
});
</script>

然而,我想要控制水平轴上显示的刻度/日期。我已经查看了Google API文档,并找到了我认为需要的对象(hAxis.ticks),但我无论如何都无法使其起作用。无论我做什么,它对显示的日期没有任何影响。

以下是我认为是最佳代码,但它不起作用:

<script type="text/javascript">
jQuery(window).on('load',function(){ 
    if( typeof wpDataChartsCallbacks == 'undefined' ){ wpDataChartsCallbacks = {}; } 
    wpDataChartsCallbacks[4] = function(obj){ 
        obj.options.hAxis.ticks = [{v: new Date(2020, 1, 1), f: '2020'}, {v: new Date(2021, 1, 1), f: '2021'}, {v: new Date(2022, 1, 1), f: '2022'}, {v: new Date(2023, 1, 1), f: '2023'}];
    }
});
</script>

非常感谢您的帮助。请注意,我没有编程背景,所以我可能犯了很多“简单”的错误,使用了错误的编程术语等。请谅解我 自定义水平轴上的刻度/日期 – 通过wpDataTables使用Google Charts

英文:

I'm inserting a Google line chart on my site via the WP plugin wpDataTables (wpDataCharts). I'm using Elementor to build the site. You can see the chart here a little down on the page.

wpDataTables has a limited number of chart options you can adjust, probably based on what the Google API allows. If you want to make further changes, you can use the callback function.

I have used the callback function to remove the padding Google adds to the chart as standard (which makes the chart fill out only 50% of the container) and made the tooltip include both graphs. The working code looks like this:

&lt;script type=&quot;text/javascript&quot;&gt;
jQuery(window).on(&#39;load&#39;,function(){ 
    if( typeof wpDataChartsCallbacks == &#39;undefined&#39; ){ wpDataChartsCallbacks = {}; } 
    wpDataChartsCallbacks[4] = function(obj){ 
        obj.options.chartArea = {left:135,top:30,width:&#39;100%&#39;,height:&#39;75%&#39;};
	    obj.options.focusTarget = [&#39;category&#39;];
    }
});
&lt;/script&gt;

However, I want to control the ticks/dates shown on the horizontal axis. I have been looking in the Google API documentation and found the object I think I need (hAxis.ticks), but I just can't make it work. No matter what I do, it has zero impact on the dates shown.

Here's my best bet at the code, but it doesn't work:

&lt;script type=&quot;text/javascript&quot;&gt;
jQuery(window).on(&#39;load&#39;,function(){ 
    if( typeof wpDataChartsCallbacks == &#39;undefined&#39; ){ wpDataChartsCallbacks = {}; } 
    wpDataChartsCallbacks[4] = function(obj){ 
        obj.options.hAxis.ticks = hAxis: {
					ticks: [{v: new Date(2020, 1, 1), f: &#39;2020&#39;}, {v: new Date(2021, 1, 1), f: &#39;2021&#39;}, {v: new Date(2022, 1, 1), f: &#39;2022&#39;}, {v: new Date(2023, 1, 1), f: &#39;2023&#39;}]

   }
});
&lt;/script&gt;

Any help is much appreciated. Be aware that I have no coding background, so I'm probably making a lot of "easy" mistakes, getting the coding lingo all wrong etc. Bear with me please 自定义水平轴上的刻度/日期 – 通过wpDataTables使用Google Charts

答案1

得分: 0

在你提供的代码片段中,看起来在ticks数组内部有一个额外的hAxis:。尝试删除它,看看是否有效:

&lt;script type=&quot;text/javascript&quot;&gt;
jQuery(window).on(&#39;load&#39;,function(){ 
    if( typeof wpDataChartsCallbacks == &#39;undefined&#39; ){ wpDataChartsCallbacks = {}; } 
    wpDataChartsCallbacks[4] = function(obj){ 
        obj.options.hAxis.ticks = [{v: new Date(2020, 1, 1), f: &#39;2020&#39;}, {v: new Date(2021, 1, 1), f: &#39;2021&#39;}, {v: new Date(2022, 1, 1), f: &#39;2022&#39;}, {v: new Date(2023, 1, 1), f: &#39;2023&#39;}];
    }
});
&lt;/script&gt;

希望这有所帮助!

英文:

In the code snippet you provided, it appears that you have an extra hAxis: inside the ticks array. Try removing that and see if it works:

&lt;script type=&quot;text/javascript&quot;&gt;
jQuery(window).on(&#39;load&#39;,function(){ 
    if( typeof wpDataChartsCallbacks == &#39;undefined&#39; ){ wpDataChartsCallbacks = {}; } 
    wpDataChartsCallbacks[4] = function(obj){ 
        obj.options.hAxis.ticks = [{v: new Date(2020, 1, 1), f: &#39;2020&#39;}, {v: new Date(2021, 1, 1), f: &#39;2021&#39;}, {v: new Date(2022, 1, 1), f: &#39;2022&#39;}, {v: new Date(2023, 1, 1), f: &#39;2023&#39;}];
    }
});
&lt;/script&gt;

Let me know if this helps!

huangapple
  • 本文由 发表于 2023年7月23日 15:39:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747118.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定