在不同宽度的可排序 div 中使用 amchart

Use amchart in sortable div with different width

我有一个用 bootstrap 制作的页面,我在这个页面中有 2 个不同的部分,第一个使用 "col-lg-12",第二个使用 "col-lg-6",它们都是可拖动的。我想在其中放置一些 amcharts,一切正常,我的图表响应迅速,但是当我将大截面中的 div 移动到小截面图表时,图表响应功能停止工作并且不会自动调整大小。 我的 html 代码

<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/material.js"></script>
<script src="https://www.amcharts.com/lib/4/themes/animated.js"></script>
<div class="row" id="kt_sortable_portlets">
  <div class="col-lg-12">
    <!--begin::Portlet-->
    <div class="kt-portlet kt-portlet--sortable">
      <div class="kt-portlet__head">
        <div class="kt-portlet__head-label">
          <h3 class="kt-portlet__head-title">
            Big div
          </h3>
        </div>

      </div>
      <div class="kt-portlet__body">
        <div id="kt_chart1" style="min-height: 300px;">
        </div>
      </div>
    </div>
    <!--end::Portlet-->

    <!-- begin:Empty Portlet: sortable porlet required for each columns! -->
    <div style="border:5px dashed rgb(54, 161, 233);visibility: visible;text-align: center" class="kt-portlet kt-portlet--sortable-empty">
      <h5 style="margin: auto auto">Drag here to Full screen</h5>
    </div>

    <!--end::Empty Portlet-->
  </div>
  <div class="col-lg-6">
    <!--begin::Portlet-->
    <div class="kt-portlet kt-portlet--sortable">
      <div class="kt-portlet__head">
        <div class="kt-portlet__head-label">
          <h3 class="kt-portlet__head-title">
            small div
          </h3>
        </div>
      </div>
      <div class="kt-portlet__body">
        <div id="kt_flotcharts_heart" style="min-height: 300px;">
        </div>
      </div>
    </div>
    <!-- begin:Empty Portlet: sortable porlet required for each columns! -->
    <div class="kt-portlet kt-portlet--sortable-empty"></div>
    </div>

    <!--end::Portlet-->
    <script>
     var KTAppOptions = {"colors":{"state": 
 {"brand":"#5d78ff","light":"#ffffff","dark":"#282a3c","primary":"#5867dd","success":"#34bfa3","info":"#36a3f7","warning":"#ffb822","danger":"#fd3995"},"base":{"label":["#c5cbe3","#a1a8c3","#3d4465","#3e4466"],"shape":["#f0f3ff","#d9dffa","#afb4d4","#646c9a"]}}};
        </script>

我的js代码

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

var chart = am4core.create("kt_chart1", am4charts.XYChart);
var drawChart=function(){
var data = [];
var open = 100;
var close = 120;

var names = ["Raina",
  "Demarcus",
  "Carlo",
  "Jacinda",
  "Richie",
  "Antony",
  "Amada",
  "Idalia",
  "Janella",
  "Marla",
  "Curtis",
  "Shellie",
  "Meggan",
  "Nathanael",
  "Jannette",
  "Tyrell",
  "Sheena",
  "Maranda",
  "Briana",
  "Rosa",
  "Rosanne",
  "Herman",
  "Wayne",
  "Shamika",
  "Suk",
  "Clair",
  "Olivia",
  "Hans",
  "Glennie",
];

for (var i = 0; i < names.length; i++) {
  open += Math.round((Math.random() < 0.5 ? 1 : -1) * Math.random() * 5);
  close = open + Math.round(Math.random() * 10) + 3;
  data.push({ category: names[i], open: open, close: close });
}

chart.data = data;
var categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
categoryAxis.renderer.grid.template.location = 0;
categoryAxis.dataFields.category = "category";
categoryAxis.renderer.minGridDistance = 15;
categoryAxis.renderer.grid.template.location = 0.5;
categoryAxis.renderer.grid.template.strokeDasharray = "1,3";
categoryAxis.renderer.labels.template.rotation = -90;
categoryAxis.renderer.labels.template.horizontalCenter = "left";
categoryAxis.renderer.labels.template.location = 0.5;
categoryAxis.renderer.inside = true;

categoryAxis.renderer.labels.template.adapter.add("dx", (dx, target)=>{
    return -target.maxRight / 2;
})

var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
valueAxis.tooltip.disabled = true;
valueAxis.renderer.ticks.template.disabled = true;
valueAxis.renderer.axisFills.template.disabled = true;

var series = chart.series.push(new am4charts.ColumnSeries());
series.dataFields.categoryX = "category";
series.dataFields.openValueY = "open";
series.dataFields.valueY = "close";
series.tooltipText = "open: {openValueY.value} close: {valueY.value}";
series.sequencedInterpolation = true;
series.fillOpacity = 0;
series.strokeOpacity = 1;
series.columns.template.width = 0.01;
series.tooltip.pointerOrientation = "horizontal";

var openBullet = series.bullets.create(am4charts.CircleBullet);
openBullet.locationY = 1;

var closeBullet = series.bullets.create(am4charts.CircleBullet);

closeBullet.fill = chart.colors.getIndex(4);
closeBullet.stroke = closeBullet.fill;

chart.cursor = new am4charts.XYCursor();

chart.scrollbarX = new am4core.Scrollbar();
chart.scrollbarY = new am4core.Scrollbar();
}

var KTPortletDraggable = function () {

    return {
        //main function to initiate the module
        init: function () {
            $("#kt_sortable_portlets").sortable({
                connectWith: ".kt-portlet__head",
                items: ".kt-portlet",
                opacity: 0.8,
                handle : '.kt-portlet__head',
                coneHelperSize: true,
                placeholder: 'kt-portlet--sortable-placeholder',
                forcePlaceholderSize: true,
                tolerance: "pointer",
                helper: "clone",
                tolerance: "pointer",
                forcePlaceholderSize: !0,
                helper: "clone",
                cancel: ".kt-portlet--sortable-empty", // cancel dragging if portlet is in fullscreen mode
                revert: 250, // animation in milliseconds
                update: function(b, c) {
                    if (c.item.prev().hasClass("kt-portlet--sortable-empty")) {
                        c.item.prev().before(c.item);
                    }                    
                }
            });
        }
    };
}();

jQuery(document).ready(function() {
drawChart();
    KTPortletDraggable.init();
});

这是我的完整代码: https://jsfiddle.net/primemb/va0n4xs3/6/

我不清楚到底是什么阻止了图表调整大小。

当您开始拖动大 div 时,图表父级的父级 div (.kt-portlet) 得到 display: none;。停止拖动后,它会设置回 flexbox 并且图表不再自动调整大小(尝试通过 devtools 手动更新 #kt_chart1 的宽度)。

但是,如果您手动将图表 div 的父级 (.kt-portlet__body) 或父级的父级的显示模式重置为 display: block;,然后不久之后将其删除,该图表可以再次 auto-resize。

因此,希望不会触发不希望的 incorrectly-styled 内容闪现的一种骇人听闻的方法是在 stop event of the slider 之后的短时间内执行上述操作:

            stop: function(event, ui) {
              setTimeout(function() {
                ui.item.css('display', 'block');
              }, 10);
              setTimeout(function() {
                ui.item.css('display', '');
              }, 20);
            },

分叉: https://jsfiddle.net/notacouch/w5qjpcs1/3/

顺便说一句,这是 [y] 我们的 GitHub 问题的副本:

https://github.com/amcharts/amcharts4/issues/1341