【问题标题】:How do I get rid of this amCharts distortion?如何摆脱这种 amCharts 失真?
【发布时间】:2019-02-23 18:37:06
【问题描述】:

我想使用 amCharts Javascript 库显示我的用户排名。用户排名是这样的(很好,没有问题):

问题是,如果我有两个相同的个人资料名称(例如,如果我有两个 Sara 名称作为个人资料名称,甚至有两个相似的口号句子),就会发生这样的事情:(这里我将 John 更改为 Sara,所以我们有两个 Saras)

注意:整个文本区域如箭头所示向下。似乎我失去了其中一个 Sara(现在我只能看到 5 个配置文件,而不是 6 个配置文件)。

另外,我在 amCharts 演示中发现了 this codethis one,这似乎是一个解决方案或提示。

Here is my code on CodePen.

这是我的 javascript 代码:

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


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

chart.data = [{
        "profileName": "John",
        "slogan": "Slogan Sentence Related to Profile Name 6",
        "level": "6.",
        "income": 0,
        "bullet": "https://www.scripturaengage.com/wp-content/uploads/2017/05/Profile-Picture-Koen-VanGeert-circle-ScripturaEngage.png"

    }, {
        "profileName": "Sara",
        "slogan": "Slogan Sentence Related to Profile Name 5",
        "level": "5.",
        "income": 0,
        "bullet": "https://i1.wp.com/teacupwellness.com/wp-content/uploads/2018/03/Amanda-Blankinship-profile-circle.png?fit=300%2C300&ssl=1"
    }, {
        "profileName": "Nima",
        "slogan": "Slogan Sentence Related to Profile Name 4",
        "level": "4.",
        "income": 0,
        "bullet": "https://www.scripturaengage.com/wp-content/uploads/2017/05/Profile-Picture-Pauline-Suy-circle-ScripturaEngage.png"
    }, {
        "profileName": "Max",
        "slogan": "Slogan Sentence Related to Profile Name 3",
        "level": "3.",
        "income": 0,
        "bullet": "https://www.scripturaengage.com/wp-content/uploads/2017/05/Profile-Picture-Koen-VanGeert-circle-ScripturaEngage.png"
    }, {
        "profileName": "Megan",
        "slogan": "Slogan Sentence Related to Profile Name 2",
        "level": "2.",
        "income": 0,
        "bullet": "https://i1.wp.com/teacupwellness.com/wp-content/uploads/2018/03/Amanda-Blankinship-profile-circle.png?fit=300%2C300&ssl=1"
    }, {
        "profileName": "Inna",
        "slogan": "Slogan Sentence Related to Profile Name 1",
        "level": "1.",
        "income": 0,
        "bullet": "https://www.scripturaengage.com/wp-content/uploads/2017/05/Profile-Picture-Pauline-Suy-circle-ScripturaEngage.png"
    }  ];

// Chart A:

//create category axis for Names Column 
var categoryAxis = chart.yAxes.push(new am4charts.CategoryAxis());
categoryAxis.dataFields.category = "profileName";
categoryAxis.renderer.grid.template.location = -100;
categoryAxis.renderer.inside = true;
categoryAxis.renderer.labels.template.dy = -60;
categoryAxis.renderer.labels.template.dx = 200;
categoryAxis.labelsEnabled = false;
categoryAxis.renderer.labels.template.fontSize = 20;
categoryAxis.renderer.labels.template.fill = am4core.color("#4286f4");


//create category axis for Slogans Column 
var categoryAxis2 = chart.yAxes.push(new am4charts.CategoryAxis());
categoryAxis2.dataFields.category = "slogan";
categoryAxis2.renderer.grid.template.location = 100;
categoryAxis2.renderer.inside = true;
categoryAxis2.renderer.labels.template.dy = -20;
categoryAxis2.renderer.labels.template.dx = 200;
categoryAxis2.renderer.labels.template.fontSize = 12;

//create category level
var categoryAxis3 = chart.yAxes.push(new am4charts.CategoryAxis());
categoryAxis3.dataFields.category = "level";
categoryAxis3.renderer.grid.template.location = 100;
categoryAxis3.renderer.inside = true;
categoryAxis3.renderer.labels.template.dy = -20;
categoryAxis3.renderer.labels.template.dx = 170;
categoryAxis3.renderer.labels.template.fontSize = 16;
categoryAxis3.renderer.labels.template.fill = am4core.color("#ccd3e0");


//create value axis for income and expenses
var valueAxis = chart.xAxes.push(new am4charts.ValueAxis());
valueAxis.renderer.opposite = true;



valueAxis.min = 0;
valueAxis.max = 10;
valueAxis.strictMinMax = true;  
valueAxis.renderer.minGridDistance = 25;
valueAxis.renderer.labels.template.disabled = true;
valueAxis.disabled = true;

//create columns
var series = chart.series.push(new am4charts.ColumnSeries());
series.dataFields.categoryY = "profileName";
series.dataFields.valueX = "income";
series.name = "Income";
series.columns.template.fillOpacity = 0.5;
series.columns.template.strokeOpacity = 0;


// Do not crop bullets
chart.maskBullets = false;
chart.paddingTop = 64;
chart.paddingBottom = -30;

// Add bullets
var bullet = series.bullets.push(new am4charts.Bullet());
var image = bullet.createChild(am4core.Image);
image.horizontalCenter = "middle";
image.verticalCenter = "bottom";
image.width = 120;
image.height = 120;
image.dy = 0;
image.dx = 100;
image.y = am4core.percent(100);
image.propertyFields.href = "bullet";
image.tooltipText = series.columns.template.tooltipText;
image.propertyFields.fill = "color";
image.filters.push(new am4core.DropShadowFilter());

【问题讨论】:

    标签: javascript html amcharts


    【解决方案1】:

    CategoryAxis 是一个类别轴。这意味着图表上的项目按“类别”分组,在您的情况下为 profileName

    如果您必须使用相同的类别(例如两个 Saras),它们将被分组到一个插槽中,因此您遇到了问题。

    唯一的解决方案是确保所有类别都是唯一的。例如,您可以在每个名称后加上 "~[userid]"

    然后添加一个去除后缀的适配器:

    // Themes begin
    am4core.useTheme(am4themes_animated);
    // Themes end
    
    
    var chart = am4core.create("chartdiv", am4charts.XYChart);
    
    chart.data = [{
            "profileName": "Sara~2",
    		"slogan": "Slogan Sentence Related to Profile Name 6",
    		"level": "6.",
            "income": 0,
    		"bullet": "https://www.scripturaengage.com/wp-content/uploads/2017/05/Profile-Picture-Koen-VanGeert-circle-ScripturaEngage.png"
    
        }, {
            "profileName": "Sara",
    		"slogan": "Slogan Sentence Related to Profile Name 5",
    		"level": "5.",
            "income": 0,
    		"bullet": "https://i1.wp.com/teacupwellness.com/wp-content/uploads/2018/03/Amanda-Blankinship-profile-circle.png?fit=300%2C300&ssl=1"
        }, {
            "profileName": "Nima",
    		"slogan": "Slogan Sentence Related to Profile Name 4",
    		"level": "4.",
            "income": 0,
    		"bullet": "https://www.scripturaengage.com/wp-content/uploads/2017/05/Profile-Picture-Pauline-Suy-circle-ScripturaEngage.png"
        }, {
            "profileName": "Max",
    		"slogan": "Slogan Sentence Related to Profile Name 3",
    		"level": "3.",
            "income": 0,
    		"bullet": "https://www.scripturaengage.com/wp-content/uploads/2017/05/Profile-Picture-Koen-VanGeert-circle-ScripturaEngage.png"
        }, {
            "profileName": "Megan",
    		"slogan": "Slogan Sentence Related to Profile Name 2",
    		"level": "2.",
            "income": 0,
    		"bullet": "https://i1.wp.com/teacupwellness.com/wp-content/uploads/2018/03/Amanda-Blankinship-profile-circle.png?fit=300%2C300&ssl=1"
        }, {
            "profileName": "Inna",
    		"slogan": "Slogan Sentence Related to Profile Name 1",
    		"level": "1.",
            "income": 0,
    		"bullet": "https://www.scripturaengage.com/wp-content/uploads/2017/05/Profile-Picture-Pauline-Suy-circle-ScripturaEngage.png"
        }  ];
    	
    // Chart A:
    
    //create category axis for Names Column 
    var categoryAxis = chart.yAxes.push(new am4charts.CategoryAxis());
    categoryAxis.dataFields.category = "profileName";
    categoryAxis.renderer.grid.template.location = -100;
    categoryAxis.renderer.inside = true;
    categoryAxis.renderer.labels.template.dy = -60;
    categoryAxis.renderer.labels.template.dx = 200;
    categoryAxis.labelsEnabled = false;
    categoryAxis.renderer.labels.template.fontSize = 20;
    categoryAxis.renderer.labels.template.fill = am4core.color("#4286f4");
    categoryAxis.renderer.labels.template.adapter.add("textOutput", stripSuffix);
    
    
    //create category axis for Slogans Column 
    var categoryAxis2 = chart.yAxes.push(new am4charts.CategoryAxis());
    categoryAxis2.dataFields.category = "slogan";
    categoryAxis2.renderer.grid.template.location = 100;
    categoryAxis2.renderer.inside = true;
    categoryAxis2.renderer.labels.template.dy = -20;
    categoryAxis2.renderer.labels.template.dx = 200;
    categoryAxis2.renderer.labels.template.fontSize = 12;
    categoryAxis2.renderer.labels.template.adapter.add("textOutput", stripSuffix);
    
    //create category level
    var categoryAxis3 = chart.yAxes.push(new am4charts.CategoryAxis());
    categoryAxis3.dataFields.category = "level";
    categoryAxis3.renderer.grid.template.location = 100;
    categoryAxis3.renderer.inside = true;
    categoryAxis3.renderer.labels.template.dy = -20;
    categoryAxis3.renderer.labels.template.dx = 170;
    categoryAxis3.renderer.labels.template.fontSize = 16;
    categoryAxis3.renderer.labels.template.fill = am4core.color("#ccd3e0");
    
    function stripSuffix(category) {
      return category.split("~").shift();
    }
    
    
    //create value axis for income and expenses
    var valueAxis = chart.xAxes.push(new am4charts.ValueAxis());
    valueAxis.renderer.opposite = true;
    
    
    
    valueAxis.min = 0;
    valueAxis.max = 10;
    valueAxis.strictMinMax = true;  
    valueAxis.renderer.minGridDistance = 25;
    valueAxis.renderer.labels.template.disabled = true;
    valueAxis.disabled = true;
    
    //create columns
    var series = chart.series.push(new am4charts.ColumnSeries());
    series.dataFields.categoryY = "profileName";
    series.dataFields.valueX = "income";
    series.name = "Income";
    series.columns.template.fillOpacity = 0.5;
    series.columns.template.strokeOpacity = 0;
    
    
    // Do not crop bullets
    chart.maskBullets = false;
    chart.paddingTop = 64;
    chart.paddingBottom = -30;
    
    // Add bullets
    var bullet = series.bullets.push(new am4charts.Bullet());
    var image = bullet.createChild(am4core.Image);
    image.horizontalCenter = "middle";
    image.verticalCenter = "bottom";
    image.width = 120;
    image.height = 120;
    image.dy = 0;
    image.dx = 100;
    image.y = am4core.percent(100);
    image.propertyFields.href = "bullet";
    image.tooltipText = series.columns.template.tooltipText;
    image.propertyFields.fill = "color";
    image.filters.push(new am4core.DropShadowFilter());
    #chartdiv {
      width: 1500px;
      height: 840px;
    }
    
    body {	
    	margin: 40 100 100 20;
    	background-color: transparent;
        overflow:hidden;
      font-family: "Helvetica Neue";
      font-weight: 800;
      src: url(helveticaneue-ultrathin.woff);	
     }
    <!-- Resources -->
    <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>

    【讨论】:

      猜你喜欢
      • 2012-01-15
      • 2017-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-09
      • 1970-01-01
      • 2022-07-30
      相关资源
      最近更新 更多