JQPlot -基于区域的格式刻度

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

How can I format the ticks in my jqplot to handle the change between , and . and " " depending on the region I'm in.

如何在jqplot中格式化蜱虫,以处理和之间的更改。"取决于我所在的地区。

For example.

为例。

  • European - 1.000,00
  • 欧洲- 1.000,00
  • US - 1,000.00
  • - 1000 .00美元
  • Other 1 000,00
  • 其他1 000,00

My example jqplot initialization looks like....

我的例子jqplot初始化看起来像....

plot3 = $.jqplot('saturationChart', lineArray, 
         { 
          title:m_Language.saturationChartName,
          series:lineColor,
          legend:{show:true, location:'se'},
          // You can specify options for all axes on the plot at once with
          // the axesDefaults object.  Here, we're using a canvas renderer
          // to draw the axis label which allows rotated text.
          axes:{
            xaxis:{
//            label:'Minutes',
              renderer: $.jqplot.LogAxisRenderer,
              tickDistribution:'power',
              labelRenderer: $.jqplot.CanvasAxisLabelRenderer,
              labelOptions: {
                //fontSize: '12pt'
              },
            },
            yaxis:{
//            label:'Megaohms',
              renderer: $.jqplot.LogAxisRenderer,
              tickDistribution:'power',
              labelRenderer: $.jqplot.CanvasAxisLabelRenderer,
              labelOptions: {
                //fontSize: '12pt'
              }
            },
          }
      });

I looked through the documentation but I am not sure what keywords I need to look for... any help would be appreciated

我查看了文档,但我不确定需要查找什么关键字……如有任何帮助,我们将不胜感激

1 个解决方案

#1


12  

Based on this, you could use a format string like the following:

基于此,您可以使用如下格式字符串:

yaxis: {
    tickOptions: { formatString: "%'.2f" },
    min : 0
}

That format string, along with the value, end up as arguments into a call to $.jqplot.sprintf. You can then set the separators as follows:

该格式字符串和值一起作为参数调用$.jqplot.sprintf。你可以设置分隔符如下:

$.jqplot.sprintf.thousandsSeparator = ',';
$.jqplot.sprintf.decimalMark = '.';

So this code:

所以这段代码:

var value = 10000;
$.jqplot.sprintf.thousandsSeparator = '.';
$.jqplot.sprintf.decimalMark = ',';
console.log($.jqplot.sprintf("European %'.2f", value));

$.jqplot.sprintf.thousandsSeparator = ' ';
$.jqplot.sprintf.decimalMark = ',';
console.log($.jqplot.sprintf("Other %'.2f", value));

$.jqplot.sprintf.thousandsSeparator = ',';
$.jqplot.sprintf.decimalMark = '.';
console.log($.jqplot.sprintf("US %'.2f", value));

Will produce this:

会产生:

European 10,000.00 
Other 10 000,00 
US 10,000.00 

However, see how the European one is the same as the US? When those separator settings are changed, it results in string replacement, hence why the European one ends up like it does: replacing commas with periods and then periods with commas gives you back the original string.

然而,看看欧洲和美国有何不同?当这些分隔符设置被更改时,将导致字符串替换,因此,欧洲的分隔符以句点替换逗号,然后以逗号替换句点,从而返回原来的字符串。

Because of that, you would need to provide a custom tick formatting method so you could apply those settings on-the-fly and then do a string replace:

因此,您需要提供一个自定义的蜱虫格式化方法,以便您可以即时应用这些设置,然后执行字符串替换:

yaxis: {
    tickOptions: {
        tickOptions: { formatString: "%'.2f" },
        formatter: function(format, value){
            return FormatTick(format, value);
        }
    }
}

....

function FormatTick(format, value) {
    var prevThousandsSeparator = $.jqplot.sprintf.thousandsSeparator;
    var prevDecimalMark = $.jqplot.sprintf.decimalMark;

    $.jqplot.sprintf.thousandsSeparator = '#';
    $.jqplot.sprintf.decimalMark = '_';

    var formattedValue = $.jqplot.sprintf(format, value);
    formattedValue = formattedValue.replace($.jqplot.sprintf.decimalMark, Language.DecimalMark)
        .replace($.jqplot.sprintf.thousandsSeparator, Language.ThousandsSeparator);

    $.jqplot.sprintf.thousandsSeparator = prevThousandsSeparator;
    $.jqplot.sprintf.decimalMark = prevDecimalMark;

    return formattedValue;
}

This means you will also need some convenient mechanism for establishing both the locale of the user and the correct separator characters to use. I save and restore the thousandsSeparator and decimalMark characters in this way just to ensure that setting them in this way does not cause inadvertant problems elsewhere.

这意味着您还需要一些方便的机制来建立用户的语言环境和正确的分隔符。我以这种方式保存和恢复千位分隔符和小数标记字符,以确保以这种方式设置它们不会引起其他地方的意外问题。

Note also that my solution assumes that the # and _ characters will not otherwise be in the value you are wanting to format. If this is not so, modify the separators in the FormatTick method to something more suitable.

还要注意,我的解决方案假设#和_字符在其他情况下不在您想要格式化的值中。如果不是这样,请将FormatTick方法中的分隔符修改为更合适的类型。

#1


12  

Based on this, you could use a format string like the following:

基于此,您可以使用如下格式字符串:

yaxis: {
    tickOptions: { formatString: "%'.2f" },
    min : 0
}

That format string, along with the value, end up as arguments into a call to $.jqplot.sprintf. You can then set the separators as follows:

该格式字符串和值一起作为参数调用$.jqplot.sprintf。你可以设置分隔符如下:

$.jqplot.sprintf.thousandsSeparator = ',';
$.jqplot.sprintf.decimalMark = '.';

So this code:

所以这段代码:

var value = 10000;
$.jqplot.sprintf.thousandsSeparator = '.';
$.jqplot.sprintf.decimalMark = ',';
console.log($.jqplot.sprintf("European %'.2f", value));

$.jqplot.sprintf.thousandsSeparator = ' ';
$.jqplot.sprintf.decimalMark = ',';
console.log($.jqplot.sprintf("Other %'.2f", value));

$.jqplot.sprintf.thousandsSeparator = ',';
$.jqplot.sprintf.decimalMark = '.';
console.log($.jqplot.sprintf("US %'.2f", value));

Will produce this:

会产生:

European 10,000.00 
Other 10 000,00 
US 10,000.00 

However, see how the European one is the same as the US? When those separator settings are changed, it results in string replacement, hence why the European one ends up like it does: replacing commas with periods and then periods with commas gives you back the original string.

然而,看看欧洲和美国有何不同?当这些分隔符设置被更改时,将导致字符串替换,因此,欧洲的分隔符以句点替换逗号,然后以逗号替换句点,从而返回原来的字符串。

Because of that, you would need to provide a custom tick formatting method so you could apply those settings on-the-fly and then do a string replace:

因此,您需要提供一个自定义的蜱虫格式化方法,以便您可以即时应用这些设置,然后执行字符串替换:

yaxis: {
    tickOptions: {
        tickOptions: { formatString: "%'.2f" },
        formatter: function(format, value){
            return FormatTick(format, value);
        }
    }
}

....

function FormatTick(format, value) {
    var prevThousandsSeparator = $.jqplot.sprintf.thousandsSeparator;
    var prevDecimalMark = $.jqplot.sprintf.decimalMark;

    $.jqplot.sprintf.thousandsSeparator = '#';
    $.jqplot.sprintf.decimalMark = '_';

    var formattedValue = $.jqplot.sprintf(format, value);
    formattedValue = formattedValue.replace($.jqplot.sprintf.decimalMark, Language.DecimalMark)
        .replace($.jqplot.sprintf.thousandsSeparator, Language.ThousandsSeparator);

    $.jqplot.sprintf.thousandsSeparator = prevThousandsSeparator;
    $.jqplot.sprintf.decimalMark = prevDecimalMark;

    return formattedValue;
}

This means you will also need some convenient mechanism for establishing both the locale of the user and the correct separator characters to use. I save and restore the thousandsSeparator and decimalMark characters in this way just to ensure that setting them in this way does not cause inadvertant problems elsewhere.

这意味着您还需要一些方便的机制来建立用户的语言环境和正确的分隔符。我以这种方式保存和恢复千位分隔符和小数标记字符,以确保以这种方式设置它们不会引起其他地方的意外问题。

Note also that my solution assumes that the # and _ characters will not otherwise be in the value you are wanting to format. If this is not so, modify the separators in the FormatTick method to something more suitable.

还要注意,我的解决方案假设#和_字符在其他情况下不在您想要格式化的值中。如果不是这样,请将FormatTick方法中的分隔符修改为更合适的类型。