jquery 如何显示/隐藏具有顶点图的div动画

daupos2t  于 2023-08-04  发布在  jQuery
关注(0)|答案(2)|浏览(108)

善良的Afternnon
嗨,我是新的编码和学习在线家庭项目。
我使用Apexchart来显示持续时间的数据。我根据持续时间数据制作了4个div。从下拉菜单中选择持续时间后,我需要隐藏除选定div之外的所有div。我尝试使用显示和隐藏,它的部分工作。假设我首先选择“上个月”,它会显示右边的div,然后我选择“本月”,它会隐藏上个月的div并显示本月的div。但是当我重新选择“上个月”div时,没有显示图表。
是有任何替代,我可以显示div与动画更好的方式。
先谢了
代码

<div id="totalMilksaleChartfor7days"></div>

<div id="totalMilksaleChartcurrentmonth" style="display:none"></div>

<div id="totalMilksaleChartlastmonth" style="display:none"></div>

<div id="totalMilksaleChart" style="display:none"></div>


 var durationget = duration;

      if (durationget == "currentmonth") {

        $("#totalMilksaleChartcurrentmonth").show();

        $("#totalMilksaleChartfor7days").hide();
        $("#totalMilksaleChartlastmonth").hide();
        $("#totalMilksaleChart").hide();

      } else if (durationget == "lastmonth") {

        $("#totalMilksaleChartlastmonth").show();

        $("#totalMilksaleChartfor7days").hide();
        $("#totalMilksaleChartcurrentmonth").hide();
        $("#totalMilksaleChart").hide();

      } else if (durationget == "currentyear") {

        $("#totalMilksaleChart").show();

        $("#totalMilksaleChartfor7days").hide();
        $("#totalMilksaleChartlastmonth").hide();
        $("#totalMilksaleChartcurrentmonth").hide();

      } else {

        $("#totalMilksaleChartfor7days").show();

        $("#totalMilksaleChartlastmonth").hide();
        $("#totalMilksaleChartcurrentmonth").hide();
        $("#totalMilksaleChart").hide();

      }

字符串

o75abkj4

o75abkj41#

我想不出为什么它不会工作,接缝足够简单:

function update() {
  const durationget = $("#duration").val();
  $("#totalMilksaleChartfor7days, #totalMilksaleChartcurrentmonth, #totalMilksaleChartlastmonth, #totalMilksaleChart").hide();
  if (durationget == "currentmonth") {
    $("#totalMilksaleChartcurrentmonth").show();
  } else if (durationget == "lastmonth") {
    $("#totalMilksaleChartlastmonth").show();
  } else if (durationget == "currentyear") {
    $("#totalMilksaleChart").show();
  } else {
    $("#totalMilksaleChartfor7days").show();
  }
}

个字符

qnyhuwrf

qnyhuwrf2#

ApexCharts提供了一个API,允许您动态更新图表数据。

<button id="toggleButton">Toggle Chart</button>
<div id="chartContainer" class="chart-container">
 <div id="chart"></div>
</div>

<style>
 .chart-container {
   max-height: 0;
   overflow: hidden;
   transition: max-height 0.3s ease;
 }
</style>

<script>
function toggleChartContainer() {
   const chartContainer = document.getElementById("chartContainer");
   const isContainerVisible = chartContainer.style.maxHeight !== "0px";

   if (isContainerVisible) {
      // If the container is visible, hide it
      chartContainer.style.maxHeight = "0px";
   } else {
      // If the container is hidden, show it
      chartContainer.style.maxHeight = "500px"; // Set your desired height here
      // Call the function to render the chart (if it hasn't been rendered yet)
      renderChart();
   }
}

// Function to render the Apex chart
function renderChart() {
  const options = {
  // Your Apex chart options here
};
 const chartData = {
  // Your Apex chart data here
};

// Check if the chart has been initialized already
if (typeof chart === "undefined") {
// If not, create the chart
chart = new ApexCharts(document.querySelector("#chart"), options);
chart.render();
} else {
// If the chart has been initialized, update the data
chart.updateSeries(chartData);
}
}

// Event listener for the toggle button
document.getElementById("toggleButton").addEventListener("click", 
toggleChartContainer);
</script>

字符串

相关问题