English 中文(简体)
Multiple line on x-axis in a linear chart did not show
原标题:
The bounty expires in 6 days. Answers to this question are eligible for a +50 reputation bounty. user16780927 wants to draw more attention to this question.

I have a single axis line chart. The user would choose the year from a dropdown and the loan_type from another dropdown. The chart then should load the 12-months record of annual_payment and scheduled_payment, both as linear lines.

Dashboard.vue

<div class="card-header bg-white p-2">
    <div class="row justify-content-md-between">
        <div class="col-12 col-md-7 d-md-flex justify-content-md-end">
            <div class="input-group custom-select2">
                <div class="input-group-prepend">
                    <span class="input-group-text" id="basic-addon1">Loan Type:</span>
                </div>
                <select2 
                    id="selected_loan_type_annual_payment" 
                    name="selected_loan_type_annual_payment" 
                    class="form-control" 
                    v-model="is_inputs.annual_payment.loan_type"
                    @input="selectLoanType($event)" 
                >
                    <option 
                        v-for="(item, index) in types"
                        :key="item.id" 
                        :selected="item.id==is_inputs.annual_payment.loan_type" 
                        :value="item.id">{{ item.text }}
                    </option>
                </select2>
            </div>
        </div>
        <div class="col-12 col-md-4 d-md-flex justify-content-md-end">
            <div class="input-group custom-select2">
                <div class="input-group-prepend">
                    <span class="input-group-text" id="basic-addon1">Year:</span>
                </div>
                <select2 
                    id="selected_year_annual_payment" 
                    name="selected_year_annual_payment" 
                    class="form-control" 
                    v-model="is_inputs.annual_payment.year"
                    @input="selectPaymentYear($event)"
                >
                    <option
                        v-for="(item, index) in years"
                        :key="item.id" 
                        :selected="item.id==is_inputs.annual_payment.year" 
                        :value="item.id">{{ item.text }}
                    </option>
                </select2>
            </div>
        </div>
    </div>
</div>
<div class="card-body">
  <div class="row justify-content-center">
    <div 
      class="col-md-12" 
      v-bind:class="{
         load_chart : !is_loaded.annual_payment, 
          : is_loaded.annual_payment
      }"
    >
      <line-chart 
        v-if="is_loaded.annual_payment" 
        :width="100" :height="60" 
        :chartdata="chart_data.annual_payment" 
        :options="chart_options.annual_payment">
      </line-chart>
    </div>
  </div>
</div>

.
.

<script>
  export default {
    data() {
      return {
        is_inputs: {
          annual_payment: {
            year: "",
            loan_type: "",
           }
        },
        is_loaded: {
          annual_payment: true,
        },
        chart_options: {
          annual_payment: {
            responsive: true,
            maintainAspectRatio: true,
            legend: {
              display: true,
              position:  bottom ,
              labels: {boxWidth: 0}
            },
            scales: {
              yAxes: [{
                ticks: {
                  min: 0,
                  reverse: false,
                  beginAtZero: true
                }
              }]
            }
          }
        },
        chart_data: {
          annual_payment: {
            labels: [ Jan ,  Feb ,  Mar ,  Apr ,  May ,  Jun ,  Jul ,  Aug ,  Sep ,  Oct ,  Nov ,  Dec ],
            datasets: [
             {
              label: [],
              backgroundColor: [ #54478c ,  #2c699a ,  #048ba8 ,  #0db39e ,  #16db93 ,  #83e377 ,  #b9e769 ,  #efea5a ,  #f1c453 ,  #f35b04 ,  #bf0603 ,  #941b0c ],
              data: [],
              borderColor: "#0db39e",
              pointBorderColor: "#bf0603",
              pointBackgroundColor: "#bf0603",
              pointHoverBackgroundColor: "#bf0603",
              pointHoverBorderColor: "#bf0603",
              pointBorderWidth: 6,
              pointHoverRadius: 6,
              pointHoverBorderWidth: 1,
              pointRadius: 3,
              fill: false,
              borderWidth: 2,
              tension: 0.1
             },
             {
              label: [],
              backgroundColor: [ #2c699a ,  #54478c ,  #0db39e ,  #048ba8 ,  #83e377 ,  #16db93 ,  #efea5a , #b9e769 ,  #f35b04 ,  #f1c453 ,  #941b0c , #bf0603 ],
              data: [],
              borderColor: "#0db39F",
              pointBorderColor: "#bf0604",
              pointBackgroundColor: "#bf0604",
              pointHoverBackgroundColor: "#bf0604",
              pointHoverBorderColor: "#bf0604",
              pointBorderWidth: 6,
              pointHoverRadius: 6,
              pointHoverBorderWidth: 1,
              pointRadius: 3,
              fill: false,
              borderWidth: 2,
              tension: 0.1
             }
            ]
          }
        },
        selectLoanType(e) {
          this.is_inputs.annual_payment.loan_type = e;
          this.getCreditAnnualPayment();
        },
        selectPaymentYear(e) {
          this.is_inputs.annual_payment.year = e;
          this.getCreditAnnualPayment();
        },
        getCreditAnnualPayment() {
          this.is_loaded.annual_payment = false;
          var loan_type = this.is_inputs.annual_payment.loan_type;
          var year = this.is_inputs.annual_payment.year;
          var data = { loan_type : loan_type,  year : year};
          var url = this.routes.annual_payment;
          axios
            .post(url, data)
            .then(response => {
              var json = response.data;
              this.chart_data.annual_payment.datasets[0].label = year;
              this.chart_data.annual_payment.datasets[0].data = json;
              this.is_loaded.annual_payment = true;
            })
            .catch(err => {
              console.log( err ,err);
              this.is_loaded.annual_payment = true;
            });
        }
      }
    }
  }
</script>

AjaxDataController.php

.
.

public function annual_payment(Request $request) {
    $data = [];
    $selected_loan_type = $request->loan_type;
    $selected_year = $request->year;
    for($i=0; $i<12; $i++) {
        $dt = CarbonCarbon::createFromFormat( Y-n , $selected_year. - .($i+1))->format( Y-m );
        $payment_amount = AppPayment::where( loan_type_id , $selected_loan_type)
            ->where( paid_at ,  like , $dt. % )->sum( amount );
        $scheduled_amount = AppPayment::where( loan_type_id , $selected_loan_type)
            ->sum( scheduled_amount );
        $data[$i] = [$payment_amount, $scheduled_amount];
    }  
    return $data;
}

.
.

It would be a great help if somebody could point out where did I went wrong and how do I make the correction because at the moment the chart would not load any line.

最佳回答

In datasets you have 2 objects. Both objects have data property.

You re assigning the array of [$payment_amount, $scheduled_amount] to the first object s data property.

I think what you want to do is assign $payment_amount to the first object and $scheduled_amount to the second object.

Something like this

 var json = response.data;
 this.chart_data.annual_payment.datasets[0].label = year;
 this.chart_data.annual_payment.datasets[0].data = json[0];
 this.chart_data.annual_payment.datasets[1].data = json[1];
 this.is_loaded.annual_payment = true;
问题回答

AjaxDataController.php

public function annual_payment(Request $request) {
    $data = [];
    $data1 = [];
    $data2 = [];
    $selected_loan_type = $request->loan_type;
    $selected_year = $request->year;

    for($i=0; $i<12; $i++) {
        $dt = CarbonCarbon::createFromFormat( Y-n , $selected_year. - .($i+1))->format( Y-m );
        $payment_amount = AppPayment::where( loan_type_id , $selected_loan_type)
            ->where( paid_at ,  like , $dt. % )->sum( amount );
        $scheduled_amount = AppPayment::where( loan_type_id , $selected_loan_type)
            ->sum( scheduled_amount );
        $data1[$i] = $payment_amount;
        $data2[$i] = $scheduled_amount;
    }  
    $data = [
         payment_amount  => $data1,
         monthly_amount  => $data2
    ];
    return $data;
}

Dashboard.vue

.
.
            axios
            .post(url, data)
            .then(response => {
              var json = response.data;
              this.chart_data.annual_payment.datasets[0].label = year;
              this.chart_data.annual_payment.datasets[0].data = json.payment_amount;
              this.chart_data.annual_payment.datasets[1].data = json.monthly_amount;
              this.is_loaded.annual_payment = true;
            })
            .catch(err => {
              console.log( err ,err);
              this.is_loaded.annual_payment = true;
            });
.
. 





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签