为JFreeChart动态格式化带小数的轴

时间:2016-06-21 14:14:49

标签: java decimal jfreechart number-formatting decimalformat

我正在尝试格式化我正在创建的图表的y轴(JFreeCharts)

DecimalFormat df = new DecimalFormat("0", DecimalFormatSymbols.getInstance(Locale.ENGLISH));
df.setMaximumFractionDigits(5); 
numberTickUnit = new NumberTickUnit(numberTickUnit.getSize(), df);

有些图表的范围是0-15,所以我希望数字有2位小数。有些图表的范围是0.001到0.002,所以我希望数字有5位小数。

上面的代码效果很好,有1个问题。我希望轴上的所有数字具有相同的格式(最大小数位数)。所以如果我有0.01251, 0.02000, 0.27490,我希望它显示为那样,而不是0.01251, 0.02, 0.2749。如何使整个轴具有相同的格式,而不是为每个数字单独设置。

2 个答案:

答案 0 :(得分:1)

  

我希望数字有5位小数。

使用setMinimumFractionDigits(),其中"设置数字小数部分允许的最小位数。"

image

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" rel="stylesheet" />
<section class="container">
  <div class="row">

    <div class="col-lg-4 col-md-6 event">

      <div class="card eventDisplay">
        <img class="card-img card-img-bottom img-fluid" src="http://placehold.it/750x500" alt="alt">
        <div class="card-img-overlay">
          <h4 class="card-title">Event 1</h4>
        </div>
        <ul class="list-group list-group-flush">
          <li class="list-group-item">September 20 | 9:00am - 4:00pm</li>
          <li class="list-group-item">@Rouge Valley Park</li>
        </ul>
        <div class="card-block text-xs-center">
          <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
          <a href="#" class="btn btn-secondary btn-black">Get Planning</a>
        </div>
      </div>

    </div>

    <div class="col-lg-4 col-md-6 event">

      <div class="card eventDisplay">
        <img class="card-img card-img-bottom img-fluid" src="http://placehold.it/750x500" alt="alt">
        <div class="card-img-overlay">
          <h4 class="card-title">Event 2</h4>
        </div>
        <ul class="list-group list-group-flush">
          <li class="list-group-item">September 20 | 9:00am - 4:00pm</li>
          <li class="list-group-item">@Rouge Valley Park</li>
        </ul>
        <div class="card-block text-xs-center">
          <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content. Some quick example text to build on the card title and make up the bulk of the card's content.</p>
          <a href="#" class="btn btn-secondary btn-black">Get Planning</a>
        </div>
      </div>

    </div>

    <div class="col-lg-4 col-md-6 event">

      <div class="card eventDisplay">
        <img class="card-img card-img-bottom img-fluid" src="http://placehold.it/750x500" alt="alt">
        <div class="card-img-overlay">
          <h4 class="card-title">Event 3</h4>
        </div>
        <ul class="list-group list-group-flush">
          <li class="list-group-item">September 20 | 9:00am - 4:00pm</li>
          <li class="list-group-item">@Rouge Valley Park</li>
        </ul>
        <div class="card-block text-xs-center">
          <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content. Some quick example text to build on the card title and make up the bulk of the card's content. Some quick example text to build on the card title
            and make up the bulk of the card's content.</p>
          <a href="#" class="btn btn-secondary btn-black">Get Planning</a>
        </div>
      </div>

    </div>

    <div class="col-lg-4 col-md-6 event">

      <div class="card eventDisplay">
        <img class="card-img card-img-bottom img-fluid" src="http://placehold.it/750x500" alt="alt">
        <div class="card-img-overlay">
          <h4 class="card-title">Event 4</h4>
        </div>
        <ul class="list-group list-group-flush">
          <li class="list-group-item">September 20 | 9:00am - 4:00pm</li>
          <li class="list-group-item">@Rouge Valley Park</li>
        </ul>
        <div class="card-block text-xs-center">
          <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content. Some quick example text to build on the card title and make up the bulk of the card's content.</p>
          <a href="#" class="btn btn-secondary btn-black">Get Planning</a>
        </div>
      </div>

    </div>

    <div class="col-lg-4 col-md-6 event">

      <div class="card eventDisplay">
        <img class="card-img card-img-bottom img-fluid" src="http://placehold.it/750x500" alt="alt">
        <div class="card-img-overlay">
          <h4 class="card-title">Event 5</h4>
        </div>
        <ul class="list-group list-group-flush">
          <li class="list-group-item">September 20 | 9:00am - 4:00pm</li>
          <li class="list-group-item">@Rouge Valley Park</li>
        </ul>
        <div class="card-block text-xs-center">
          <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content. Some quick example text to build on the card title and make up the bulk of the card's content. Some quick example text to build on the card title
            and make up the bulk of the card's content.</p>
          <a href="#" class="btn btn-secondary btn-black">Get Planning</a>
        </div>
      </div>

    </div>

    <div class="col-lg-4 col-md-6 event">

      <div class="card eventDisplay">
        <img class="card-img card-img-bottom img-fluid" src="http://placehold.it/750x500" alt="alt">
        <div class="card-img-overlay">
          <h4 class="card-title">Event 6</h4>
        </div>
        <ul class="list-group list-group-flush">
          <li class="list-group-item">September 20 | 9:00am - 4:00pm</li>
          <li class="list-group-item">@Rouge Valley Park</li>
        </ul>
        <div class="card-block text-xs-center">
          <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
          <a href="#" class="btn btn-secondary btn-black">Get Planning</a>
        </div>
      </div>

    </div>

    <div class="col-lg-4 col-md-6 event">

      <div class="card eventDisplay">
        <img class="card-img card-img-bottom img-fluid" src="http://placehold.it/750x500" alt="alt">
        <div class="card-img-overlay">
          <h4 class="card-title">Event 7</h4>
        </div>
        <ul class="list-group list-group-flush">
          <li class="list-group-item">September 20 | 9:00am - 4:00pm</li>
          <li class="list-group-item">@Rouge Valley Park</li>
        </ul>
        <div class="card-block text-xs-center">
          <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content. Some quick example text to build on the card title and make up the bulk of the card's content.</p>
          <a href="#" class="btn btn-secondary btn-black">Get Planning</a>
        </div>
      </div>

    </div>

    <div class="col-lg-4 col-md-6 event">

      <div class="card eventDisplay">
        <img class="card-img card-img-bottom img-fluid" src="http://placehold.it/750x500" alt="alt">
        <div class="card-img-overlay">
          <h4 class="card-title">Event 8</h4>
        </div>
        <ul class="list-group list-group-flush">
          <li class="list-group-item">September 20 | 9:00am - 4:00pm</li>
          <li class="list-group-item">@Rouge Valley Park</li>
        </ul>
        <div class="card-block text-xs-center">
          <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content. Some quick example text to build on the card title and make up the bulk of the card's content. Some quick example text to build on the card title
            and make up the bulk of the card's content.</p>
          <a href="#" class="btn btn-secondary btn-black">Get Planning</a>
        </div>
      </div>

    </div>

    <div class="col-lg-4 col-md-6 event">

      <div class="card eventDisplay">
        <img class="card-img card-img-bottom img-fluid" src="http://placehold.it/750x500" alt="alt">
        <div class="card-img-overlay">
          <h4 class="card-title">Event 9</h4>
        </div>
        <ul class="list-group list-group-flush">
          <li class="list-group-item">September 20 | 9:00am - 4:00pm</li>
          <li class="list-group-item">@Rouge Valley Park</li>
        </ul>
        <div class="card-block text-xs-center">
          <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content. Some quick example text to build on the card title and make up the bulk of the card's content.</p>
          <a href="#" class="btn btn-secondary btn-black">Get Planning</a>
        </div>
      </div>

    </div>

    <div class="col-lg-4 col-md-6 event">

      <div class="card eventDisplay">
        <img class="card-img card-img-bottom img-fluid" src="http://placehold.it/750x500" alt="alt">
        <div class="card-img-overlay">
          <h4 class="card-title">Event 10</h4>
        </div>
        <ul class="list-group list-group-flush">
          <li class="list-group-item">September 20 | 9:00am - 4:00pm</li>
          <li class="list-group-item">@Rouge Valley Park</li>
        </ul>
        <div class="card-block text-xs-center">
          <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content. Some quick example text to build on the card title and make up the bulk of the card's content. Some quick example text to build on the card title
            and make up the bulk of the card's content.</p>
          <a href="#" class="btn btn-secondary btn-black">Get Planning</a>
        </div>
      </div>

    </div>

  </div>
</section>

答案 1 :(得分:1)

我必须手动完成。

static NavigableMap<Double, String> decimalFormatMap = new TreeMap<Double, String>();
static
{
    decimalFormatMap.put(0.00001, "0.000001");
    decimalFormatMap.put(0.0001, "0.00001");
    decimalFormatMap.put(0.001, "0.0001");
    decimalFormatMap.put(0.01, "0.001");
    decimalFormatMap.put(0.1, "0.01");
    decimalFormatMap.put(1.0, "0.1");
    decimalFormatMap.put(100.0, "1");
}
NumberTickUnit numberTickUnit = * some key number *;
String decimalFormat = decimalFormatMap.get(
    decimalFormatMap.floorKey(numberTickUnit.getSize()));