在jqplot中格式化y轴的节拍/值。

时间:2022-09-15 10:40:02

I found the following thread very useful

我发现下面的线程非常有用。

http://groups.google.com/group/jqplot-users/browse_thread/thread/1986...

http://groups.google.com/group/jqplot-users/browse_thread/thread/1986..。

I would like to get all the y axis values or ticks in an array...My intention is to count the number of digits in each of the ticks and find the most frequent number of digits. Based on that im planning to truncate number of digits in the y axis ticks/values. For example : if most of the ticks have 6 digits, truncate to 3 digits and append the word (in Thousands) to the axis label.

我想要得到数组中所有的y轴值或刻度…我的目的是计算每一个滴答数的数字,并找出最频繁的数字。基于此,我计划截断y轴上的数字。例如:如果大多数的滴答声有6个数字,将其截成3个数字,并将单词(数千)附加到axis标签。

How can i achieve this? In the below function i can get the ticks one by one...but i need to get them in advance so that i can decide by what factor to truncate the number.Please ignore the function numberwithCommas.Its just an example.

我怎样才能做到这一点呢?在下面的函数中,我可以一个一个地得到刻度…但是我需要提前把它们弄出来,这样我就可以决定截断数字的因素。请忽略使用逗号的函数号。只是一个例子。

(function($) {
    $.jqplot.tickNumberFormatter = function (format, val) {
        if (typeof val == 'number') {
            if (!format) {
                format = '%.1f';
            }
            return numberWithCommas($.jqplot.sprintf(format, val));
        }
        else {
            return String(val);
        }
    };

    function numberWithCommas(x) {
        return x.toString().replace(/\B(?=(?:\d{3})+(?!\d))/g, ",");
    } 

@Boro : Thank you so much for the reply. Could you please let me know what this line does exactly?

@Boro:非常感谢你的回复。你能告诉我这条线具体是做什么用的吗?

   var ticks = $('.jqplot-' + axisName + '-tick'); 

The same line isn't working in my code. How can I use it?

在我的代码中,同样的行不工作。我如何使用它?

Can I use it to push all ticks into an array before the control enters the formatter? In other words, can I get all the ticks before the control enters the following function?

在控件进入格式化程序之前,我是否可以使用它将所有的节拍推送到一个数组中?换句话说,在控件进入下面的函数之前,我能得到所有的节拍吗?

   $.jqplot.tickNumberFormatter = function (format, val) {

The following code works brilliantly in your example. Why doesn't it work in mine :(

下面的代码在您的示例中非常出色。为什么它不在我的工作中

                var axisName = 'yaxis';
                var count = 0;
                var ticks = $('.jqplot-' + axisName + '-tick');
                for (count = 0; count <= ticks.length; count++)
                    {

                console.log($(ticks[count]).text());
                    }

1 个解决方案

#1


3  

At first I thought it would be as easy as taking the values from the plot itself using a similar approach as here. Thus, I created a sample which on click on any bar loops around the selected axis ticks. The problem with this one is that we are using 'private' variable _ticks as the ticks one is an empty array (since it must be explicitly set). This approach would not work, for example, with xaxis where we set the ticks variable.

起初,我认为使用类似的方法从图中获取值是很简单的。因此,我创建了一个示例,该示例在选中的轴上的任何一个栏上单击。这个问题的问题是,我们使用的是“私有”变量_ticks,因为它是一个空的数组(因为它必须是显式设置的)。例如,这种方法不会起作用,我们在xaxis中设置了滴答变量。

Therefore, to get all ticks, independently from them being set by you or not, I would use jQuery and grab them whenever you need them, as presented in this code. Though, thinking about it, if the formatter acts before the plot is finished the HTML elements might not be there yet to grab them, but you can test it yourself. If this approach does not work you can always adapt the first approach to act differently depending on the ticks being set or not.

因此,为了得到所有的滴答声,独立于由您设置的,我将使用jQuery并在您需要的时候获取它们,如本文所示。尽管,考虑一下,如果formatter在绘图完成之前就已经完成了,那么HTML元素可能还没有出现,但是您可以自己测试它。如果这种方法不起作用,您可以始终采用第一个方法,根据所设置的节拍不同而采取不同的操作方式。

EDIT in response to OP's EDIT:

编辑响应OP的编辑:

Could you please let me know what this line does exactly?

你能告诉我这条线具体是做什么用的吗?

Here I use jQuery to capture ticks for axis specified by its name (axisName). For this before I use it I specify the var axisName = 'yaxis'; variable. You could substitute it with: var ticks = $('.jqplot-yaxis-tick');

在这里,我使用jQuery捕获由其名称指定的轴(axisName)。在我使用它之前,我指定var axisName = 'yaxis';变量。可以用var ticks = $('.jqplot-yaxis-tick')替换它;

As it goes to the rest of your questions it might be the case that I am mentioning towards the end of my first answer, i.e. if the formatter acts before the plot is finished the HTML elements might not be there yet to grab them.

在接下来的问题中,可能是我在第一个答案末尾提到的情况,即如果在情节完成之前,格式化程序的作用,HTML元素可能还没有出现。

EDIT2:

EDIT2:

Check out this sample. It grabs the ticks inside the formatter, as you could observe there sometimes they are zero probably because the formatter is called before the ticks HTML elements are created. Therefore, just apply your algorithm when there are some ticks otherwise apply default formatting, see if this approach will work.

看看这个样品。它可以捕获格式化程序内部的滴答声,因为您可以在那里观察到,有时它们是零,这可能是因为在创建标记HTML元素之前,格式化程序被调用了。因此,当有一些其他的标记应用默认格式时,就应用你的算法,看看这个方法是否可行。

It still might not work as we do not know what is happening, for example, why in the console we have the ticksNoZeroCounter variable equal 20 which represents a number of times the formatter was called and ticks.size() !== 0. Why the variable is not equal to 21 or to some other number divisible by 7 (i.e. the number of ticks)?

它仍然可能不工作,因为我们不知道正在发生什么,例如,为什么在控制台我们有ticksNoZeroCounter变量等于20,它表示格式化程序被调用的次数和ticks.size() !== 0。为什么这个变量不等于21或者其他的数可以被7整除(也就是刻度数)?

#1


3  

At first I thought it would be as easy as taking the values from the plot itself using a similar approach as here. Thus, I created a sample which on click on any bar loops around the selected axis ticks. The problem with this one is that we are using 'private' variable _ticks as the ticks one is an empty array (since it must be explicitly set). This approach would not work, for example, with xaxis where we set the ticks variable.

起初,我认为使用类似的方法从图中获取值是很简单的。因此,我创建了一个示例,该示例在选中的轴上的任何一个栏上单击。这个问题的问题是,我们使用的是“私有”变量_ticks,因为它是一个空的数组(因为它必须是显式设置的)。例如,这种方法不会起作用,我们在xaxis中设置了滴答变量。

Therefore, to get all ticks, independently from them being set by you or not, I would use jQuery and grab them whenever you need them, as presented in this code. Though, thinking about it, if the formatter acts before the plot is finished the HTML elements might not be there yet to grab them, but you can test it yourself. If this approach does not work you can always adapt the first approach to act differently depending on the ticks being set or not.

因此,为了得到所有的滴答声,独立于由您设置的,我将使用jQuery并在您需要的时候获取它们,如本文所示。尽管,考虑一下,如果formatter在绘图完成之前就已经完成了,那么HTML元素可能还没有出现,但是您可以自己测试它。如果这种方法不起作用,您可以始终采用第一个方法,根据所设置的节拍不同而采取不同的操作方式。

EDIT in response to OP's EDIT:

编辑响应OP的编辑:

Could you please let me know what this line does exactly?

你能告诉我这条线具体是做什么用的吗?

Here I use jQuery to capture ticks for axis specified by its name (axisName). For this before I use it I specify the var axisName = 'yaxis'; variable. You could substitute it with: var ticks = $('.jqplot-yaxis-tick');

在这里,我使用jQuery捕获由其名称指定的轴(axisName)。在我使用它之前,我指定var axisName = 'yaxis';变量。可以用var ticks = $('.jqplot-yaxis-tick')替换它;

As it goes to the rest of your questions it might be the case that I am mentioning towards the end of my first answer, i.e. if the formatter acts before the plot is finished the HTML elements might not be there yet to grab them.

在接下来的问题中,可能是我在第一个答案末尾提到的情况,即如果在情节完成之前,格式化程序的作用,HTML元素可能还没有出现。

EDIT2:

EDIT2:

Check out this sample. It grabs the ticks inside the formatter, as you could observe there sometimes they are zero probably because the formatter is called before the ticks HTML elements are created. Therefore, just apply your algorithm when there are some ticks otherwise apply default formatting, see if this approach will work.

看看这个样品。它可以捕获格式化程序内部的滴答声,因为您可以在那里观察到,有时它们是零,这可能是因为在创建标记HTML元素之前,格式化程序被调用了。因此,当有一些其他的标记应用默认格式时,就应用你的算法,看看这个方法是否可行。

It still might not work as we do not know what is happening, for example, why in the console we have the ticksNoZeroCounter variable equal 20 which represents a number of times the formatter was called and ticks.size() !== 0. Why the variable is not equal to 21 or to some other number divisible by 7 (i.e. the number of ticks)?

它仍然可能不工作,因为我们不知道正在发生什么,例如,为什么在控制台我们有ticksNoZeroCounter变量等于20,它表示格式化程序被调用的次数和ticks.size() !== 0。为什么这个变量不等于21或者其他的数可以被7整除(也就是刻度数)?