Change chart language Amcharts

Asked

Viewed 330 times

0

I have the following chart

// Themes begin
am4core.useTheme(am4themes_animated);
// Themes end

// Create chart instance
var chart = am4core.create("chartdiv", am4charts.XYChart);

// Add data
chart.data = generatechartData();
function generatechartData() {
  var chartData = [];
  var firstDate = new Date();
  firstDate.setDate( firstDate.getDate() - 150 );
  var visits = -40;
  var b = 0.6;
  for ( var i = 0; i < 150; i++ ) {
    // we create date objects here. In your data, you can have date strings
    // and then set format of your dates using chart.dataDateFormat property,
    // however when possible, use date objects, as this will speed up chart rendering.
    var newDate = new Date( firstDate );
    newDate.setDate( newDate.getDate() + i );
    if(i > 80){
        b = 0.4;
    }
    visits += Math.round((Math.random()<b?1:-1)*Math.random()*10);

    chartData.push( {
      date: newDate,
      visits: visits
    } );
  }
  return chartData;
}

// Create axes
var dateAxis = chart.xAxes.push(new am4charts.DateAxis());
dateAxis.startLocation = 0.5;
dateAxis.endLocation = 0.5;

// Create value axis
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());

// Create series
var series = chart.series.push(new am4charts.LineSeries());
series.dataFields.valueY = "visits";
series.dataFields.dateX = "date";
series.strokeWidth = 3;
series.tooltipText = "{valueY.value}";
series.fillOpacity = 0.1;

// Create a range to change stroke for values below 0
var range = valueAxis.createSeriesRange(series);
range.value = 0;
range.endValue = -1000;
range.contents.stroke = chart.colors.getIndex(4);
range.contents.fill = range.contents.stroke;
range.contents.strokeOpacity = 0.7;
range.contents.fillOpacity = 0.1;

// Add cursor
chart.cursor = new am4charts.XYCursor();
chart.cursor.xAxis = dateAxis;
chart.scrollbarX = new am4core.Scrollbar();

chart.language.locale = am4lang_pt_BR;
chart.dateFormatter.language = new am4core.Language();
chart.dateFormatter.language.locale = am4lang_pt_BR;
#chartdiv {
  width: 100%;
  height: 500px;
}
<script src="https://www.amcharts.com/lib/4/lang/pt_BR.js"></script>
<script src="https://www.amcharts.com/lib/4/core.js"></script>
<script src="https://www.amcharts.com/lib/4/charts.js"></script>
<script src="https://www.amcharts.com/lib/4/themes/animated.js"></script>

<div id="chartdiv"></div>

But I would like to leave the dates in Portuguese. I tried the following:

In html file

<script src="https://www.amcharts.com/lib/4/lang/pt_BR.js"></script>

In the js file

 chart.language.locale = am4lang_pt_BR;
 chart.dateFormatter.language = new am4core.Language();
 chart.dateFormatter.language.locale = am4lang_pt_BR;

But it didn’t work, the dates are still in English.

1 answer

-1

Place the language file in the line below the file Animated.js.

For good understanding, the above explanation was enough, but by recommendations, follows more details:

The order of the script influences the final result and its code is as below

<script src="https://www.amcharts.com/lib/4/lang/pt_BR.js"></script>
<script src="https://www.amcharts.com/lib/4/core.js"></script>
<script src="https://www.amcharts.com/lib/4/charts.js"></script>
<script src="https://www.amcharts.com/lib/4/themes/animated.js"></script>

So change as follows

<script src="https://www.amcharts.com/lib/4/core.js"></script>
<script src="https://www.amcharts.com/lib/4/charts.js"></script>
<script src="https://www.amcharts.com/lib/4/themes/animated.js"></script>
<script src="https://www.amcharts.com/lib/4/lang/pt_BR.js"></script>

I hope it’s clear enough, if not, I try to send a drawing made in the Paint. Hugs.

  • This does not answer the question. When you have reputation enough, you’ll be able to leave comments on any post but until then, write only answer that no depend on more information of who asked. - Of Revision

  • Don’t answer? Did you test before stating that about my answer? I tested and it worked.

  • Your answer is very broad, Guttemberg, hence the reason for gmsantos signaling. I recommend to specify your answer better, for example: in which "line below the file".

  • André Filipe, "in" the line below, specifies the line. If it was "in some line", it would be broad. However, I believe that the answer has now been clear. If not, you can delete, or I will delete it without any problems, because at this point, not even the author of the topic should be interested in the answer. Hugs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.